1

I'm running nginx in front of my django (gunicorn) app. I want calls made to:

api.mydomain.com

to be redirected to:

localhost:8080/api

I now have this, but this obviously doesn't work:

server {
    listen     80;
    server_name  api.mydomain.com;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    location / {
        index  index.html index.htm;
        proxy_pass  http://localhost:8080/api;
    }
}

Thanks!

2 Answers 2

3

You can combine proxy pass with rewrite

server {
    listen     80;
    server_name  api.mydomain.com;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    location / {
       index  index.html index.htm;
       rewrite ^(.*)$ /api$1 break;
       proxy_pass   http://localhost:8080;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

What should I do if I also want to serve my frontend app on 'mydomain.com'?
Just create a separate server block with the server_name mydomain.com and set the root directive to the directory containing the front end files you want to serve.
1

add a new location block like this

location ~ api.mydomain.com
{
    fastcgi_pass localhost:8080;
    fastcgi_param SCRIPT_FILENAME $document_root/Django script's folder's name/$fastcgi_script_name;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.