2

I have a Django application stored at /var/www/w_gm .

Now I have used Nginx + Gunicorn to deploy it.

My default conf file at

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

Default conf file :

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 file :

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;
    }
}

Now the issue is that, when am tying in my IP address i.e, it's redirecting me to the Django app and it's working perfectly fine. But my other files which are in var/www/html doesn't get served i.e if I have <ip.addr>/work1 , it gives me error.

Now if I edit the w_gm conf file and add a suffix, let's say server_name 119.00.00.100/abc; , which obviously is wrong, my /var/www/html files start working.

I need a solution where if I type in <ip.addr>/something, then it should redirect to Django app, else serve the files which are in var/www/html.

driftking9987
  • 1,673
  • 1
  • 32
  • 63

1 Answers1

2

You currently have two servers, but it sounds like you only want one server.

<ip.addr>/something is ambiguous, so you need Nginx to look for files in one root and send requests to the proxy only if they are not found.

You would need to combine your two server blocks into something like this:

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;
}

This assumes that the /static/ URI is unambiguous, and that no URIs ending with .php are sent to the proxy. See this docuemnt for more.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • What if I have another Django Project? I mean w_gm2 stored at `/var/www/w_gm2`, how can I make that accessible? – driftking9987 Feb 05 '19 at 09:46
  • Yeah, I agree. But let's say I store the another project in `/home/w_gm2` subfolder. Would that work? Or are you saying that, in current situation it's not possible to host 2 different Django Project. Thanks. – driftking9987 Feb 05 '19 at 09:57
  • 1
    This isn't really something that can be resolved in comments. If you have two Gunicorn applications, they will have to have unique URLs. Either by subdomain (e.g. `gm2.example.com`) or subfolder (e.g. `example.com/gm2`). They can't both occupy `/` at `example.com`. – Richard Smith Feb 05 '19 at 12:25
  • can you look at this question? https://stackoverflow.com/questions/55638962/multiple-django-project-nginx – driftking9987 Apr 12 '19 at 23:07