部署Django应用后,Nginx无法从/ var / www / html提供文件

时间:2019-02-02 10:32:07

标签: django nginx

我有一个Django应用程序存储在/var/www/w_gm上。

现在,我已经使用Nginx + Gunicorn进行部署了。

我的默认conf文件位于

root@dev:/etc/nginx/sites-enabled# ls
default  w_gm

默认CONF文件:

server {
        listen 80 default_server;
        listen [::]:80 ipv6only=on default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

w_gm conf文件:

server {
    listen 80;
    server_name 119.00.00.100;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/w_gm/w_gm/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
    }
}

现在的问题是,当我输入我的IP地址时,它会将我重定向到Django应用,并且运行良好。但是我在var/www/html中的其他文件没有得到服务,即如果我有<ip.addr>/work1,它将给我错误。

现在,如果我编辑w_gm conf文件并添加后缀,例如server_name 119.00.00.100/abc;,这显然是错误的,那么我的/var/www/html文件就会开始工作。

我需要一个解决方案,如果我输入<ip.addr>/something,那么它应该重定向到Django应用,否则将提供var/www/html中的文件。

1 个答案:

答案 0 :(得分:1)

您当前有两台服务器,但是听起来您只想要一台服务器。

<ip.addr>/something含糊不清,因此您需要Nginx在一个根目录中查找文件,并仅在找不到文件时才将请求发送到代理。

您需要将两个服务器块组合成这样:

root /var/www/html;
location / {
    try_files $uri $uri/ @proxy;
}
location ~ \.php$ {
    ...
}

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /var/www/w_gm/w_gm/;
}
location @proxy {
    include proxy_params;
    proxy_pass http://unix:/var/www/w_gm/w_gm.sock;
}

这假定/static/ URI是明确的,并且没有以.php结尾的URI被发送到代理。有关更多信息,请参见this docuemnt