笔者在同一台服务器上部署了django和php,为了使两者共存而想到了将django项目部署到二级目录下,而nginx则使用的反向代理

django中uwsgi的配置文件

[uwsgi]
chdir = /var/www/html/api
module = api.wsgi
master = true
processes = 10
#socket = :8080
http = :8080
vacuum = true
pidfile = /tmp/uwsgi.pid

nginx默认是将用户的请求通过socket与django通信,如果使用了nginx的反向代理之后访问则会通过http与django通信,所以上面的配置文件中需要将socket注释掉,取而代之的则是http = :8080

主要需要修改的是nginx的配置文件

upstream django {
	server 127.0.0.1:8080;
}

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;
	server_name _;
	location / {
		try_files $uri $uri/ =404;
	}
	
	location /api {
		# 这里为django项目的配置内容
		uwsgi_pass django;
		proxy_pass http://django/api;
		proxy_redirect off;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		include /var/www/html/api/uwsgi_params;
	}

	location ^~ /api/static {
		alias /var/www/html/bill/static;
	}	

	location ~ \.php$ {
		include fastcgi.conf;
		fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}

在浏览器中则可以使用http://服务器IP/api/,访问到django项目