Nginx – Serving Static Files and Reverse Proxy to Gunicorn Server

gunicornnginx

Hello guys I want to make an nginx config for my server. I want to serve localhost for testing for now. So I set up inside a myWebsite.conf file which I include into the nginx.conf file via include /path/to/myWebsite.conf

I want to make my server block that way that it can serve the static index.html, css files and js files when the url is localhost:8080 and when the url is localhost:8080/services I want nginx to reverse proxy a gunicord server that is up and running and is serving my falcon app (back end python framework)

The way I built my server block is following

server {
  listen 8080;
  server_name localhost;
  index index.html

  location ~ / {
    root /path/to/var/www/mySite (where I have only my index.html page)
  }

  location ~ /services {
    proxy_pass http://127.0.0.1:8000; (gunicorn server running)
  }

  add_header Cache-Control no-cache; (no cache for testing reasons)
}

The result I am getting is that the server is only serving the index.html and nothing else. When I type localhost:8080/services I dont have access to my api methods on my python program. Can you help me please on which part should I change to make it play? Or I have something completely wrong in the way I am trying to do this?

Best Answer

Add some trailing slashes in where due, and probably get rid of the tilde ~ as these are to do with regluar expression matches.

Try like this:

server {
  listen 8080;
  server_name localhost;
  index index.html

  location / {
    root /path/to/var/www/mySite; # (where I have only my index.html page)
  }

  location /services/ {
    proxy_pass http://127.0.0.1:8000/; #(gunicorn server running)

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

  add_header Cache-Control no-cache; #(no cache for testing reasons)
}
Related Topic