Ubuntu16.04 nginx+uwsgi二級目錄

筆者在同一臺服務器上部署了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項目

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章