定位 Nginx 配置文件路径
$ sudo nginx -t
会列出以下信息
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
即 Nginx 默认配置文件的路径是/etc/nginx/conf/nginx.conf
打开配置文件,并在 http {} 内添加 server 字段:
server {
listen 80;
server_name localhost;
charset utf-8;
access_log /root/test/mysite/nginx_access.log;
error_log /root/test/mysite/nginx_error.log;
client_max_body_size 75M;
location /static {
alias /root/test/mysite/mysite/static;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
# server 字段说明
server {
listen 80; # 代表服务器开放80端口
server_name localhost; # 服务器地址
charset utf-8;
access_log /root/test/mysite/nginx_access.log;
error_log /root/test/mysite/nginx_error.log;
client_max_body_size 75M;
location /static {
alias /root/test/mysite/mysite/static; # 自己定义的项目引用静态文件
}
location / {
include /etc/nginx/conf/uwsgi_params;
uwsgi_pass 127.0.0.1:8000; # uWSGI绑定的监听地址,这里使用了8000端口
}
}
注:我的 django 项目路径是/root/test/mysite
启动 uWSGI 与 Nginx
1. 启动 uWSGI
sudo uwsgi --ini /root/test/mysite/uwsgi.ini
2. 启动 Nginx
nginx -c /etc/nginx/nginx.conf
注:-c 表示加载配置文件启动
并行转发
location /wechat_qyw/ {
proxy_pass http://wechat_qyw/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /wechat_qyw/ {
proxy_pass http://wechat_qyw/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /wechat_sd/ {
proxy_pass http://wechat_sd/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}