BenJ
BenJ

Reputation: 197

How to set up Django with nginx reverse proxy

I have a Django project that I have up and running with the development server at 127.0.0.1:8888. I'm trying to get it to run on my vps with nginx, so I can see it at example.com/djangoApp.

Here's my nginx.conf:

server {
    server_name example.com;
            location /otherLocation/ {
                    proxy_pass http://127.0.0.1:10000;
            }

            location /djangoApp/ {
                     proxy_pass http://127.0.0.1:8888;
            }

When I navigate to example.com/djangoApp, it throws an error: "Using the URLconf defined in djangoApp.urls, Django tried these URL patterns, in this order: /admin The current path, djangoApp/, didn't match any of these."

Can I modify the root url in settings.py to mitigate this?

Upvotes: 4

Views: 7312

Answers (2)

Sharat Naik
Sharat Naik

Reputation: 67

        server {
               server_name example.com;
                location /otherLocation/ {
                       proxy_pass http://127.0.0.1:10000/;
                }

                location /djangoApp/ {
                       proxy_pass http://127.0.0.1:8888/;
                }
         }

The above should work. You are missing the '/' at the end of the proxy_pass url

Alternatively, you can do

         server {
               server_name example.com;
                location /otherLocation {
                       proxy_pass http://127.0.0.1:10000;
                }

                location /djangoApp {
                       proxy_pass http://127.0.0.1:8888;
                }
         }

Upvotes: 2

BenJ
BenJ

Reputation: 197

I fixed this by adding to nginx.conf:

location /djangoApp {
    rewrite  ^/djangoApp/(.*) /$1 break;
    proxy_pass http://127.0.0.1:8888;
}

Thanks to this SO exchange.

Upvotes: 6

Related Questions