3

I am trying to deploy a website with nginx and gunicorn. Nginx is serving static files and acting as a proxy to gunicorn.

Here is the content of my /etc/nginx/sites-available/herbop file

server {
       listen 80;
       server_name herbop.com;

       location / {
                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;
                proxy_set_header Host $http_host;
                proxy_intercept_errors on;
                proxy_pass http://127.0.0.1:8001;
       }

       location /static/ {
                autoindex on;
                alias /opt/Herbop-Backend/app/static/;
       }
}

server {
       listen 80;
       server_name 51.254.123.234;
       return 301 $scheme://herbop.com$request_uri;
}

In this case I seem to get an infinite redirection loop.

Any idea ?

9
  • For which request(s) you get a redirection loop?
    – gxx
    Nov 9, 2015 at 21:03
  • Basically everything herbop.com, 51.254.123.234, herbop.com/static/[...] 51.254.123.234/static/[...] The url ends up being herbop.com everytime with The page isn't redirecting properly
    – Swann
    Nov 9, 2015 at 21:06
  • Which OS and nginx version are you running?
    – gxx
    Nov 9, 2015 at 21:08
  • Ubuntu 15.04 with nginx/1.6.2
    – Swann
    Nov 9, 2015 at 21:10
  • Do herbop.com and 51.254.123.234 both point to your server?
    – gxx
    Nov 9, 2015 at 21:15

1 Answer 1

4
  • DNS: Point herbop.com using an A record to 51.254.123.234. (Remove the existing http redirect.) (Note: DNS is distributed, sometimes it takes some time to propagate.)

  • Try the following nginx config:

    server {
        listen 80;
        server_name herbop.com;
    
        location /static/ {
            root /opt/Herbop-Backend/app;
        }
    
        location / {
            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;
            proxy_set_header Host $http_host;
            proxy_intercept_errors on;
            proxy_pass http://127.0.0.1:8001;
        }
    }
    
    server {
        listen 80;
        server_name 51.254.123.234;
        return 301 $scheme://herbop.com$request_uri;
    }
    
    • Notes:

      • ((If all you want to do is serving static files to visitors, like images etc., which are embedded into your *.html, you don't need autoindex. "The ngx_http_autoindex_module processes requests ending with the slash character (‘/’) and produces a directory listing.[...]" As far as I understand, you won't need this, so don't enable it.)) ((Not sure if I got you right, and of which kind your static files are.))
      • Regarding your alias directive: According to the docs: "When location matches the last part of the directive’s value:

        location /images/ {
             alias /data/w3/images/;
        }
        

        it's better to use the root directive instead":

        location /images/ {
             root /data/w3;
        }
        

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .