1

When I run

curl -I http://myapp.com/
curl -I https://myapp.com/

http returns

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Mar 2019 17:46:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT
Connection: keep-alive
ETag: "5890a6b7-264"
Accept-Ranges: bytes

whereas the https returns

HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 05 Mar 2019 17:50:29 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://www.myapp.com/

curl: (47) Maximum (50) redirects followed

I obviously can't access the domain, I get the error ERR_TOO_MANY_REDIRECTS because Let's Encrypt is set up to redirect any http requests to https.

I see two options, deactivate the https and access the site through http or figure out why the 301 Permanent Redirect is happening on the https.

I initially got rid of the 301 Permanent Redirect on http by taking out the line

return 301 https://$server_name$request_uri;

My nginx config file

server {
listen 80;
servername myapp.com www.myapp.com;
servertokens off;
}

server {
listen 443 ssl; # managed by Certbot
server_name myapp.com www.myapp.com;

ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot  

root /home/me/myapp/src/myapp;

location = /favicon.ico { access_log off; log_not_found off; }

location /static/ {
    root /home/me/myapp/src/myapp;
}

location /media/  {
    root /home/me/myapp/src/myapp;
}

location / {
    try_files $uri/ @python_django;
}

location @python_django {
    proxy_pass http://127.0.0.1:8001;
    proxy_pass_request_headers on;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
}
}

I am in DIRE need of help, please!!!

Trilla
  • 943
  • 2
  • 15
  • 31

1 Answers1

1

You can follow this alternate approach. I find it a bit simpler to handle redirections.

server {
    listen 80;
    #listen [::]:80 ipv6only=on;

    server_name your.server.com;
    access_log /etc/nginx/access.log;

    root /var/www/html/someroot;

    location / {
            #autoindex on;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri =404;

            #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_set_header X-NginX-Proxy true;
            #proxy_pass http://127.0.0.1:8080/;
            #proxy_redirect off;
            #proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "upgrade";

            #proxy_redirect off;
            #proxy_set_header   X-Forwarded-Proto $scheme;
            #proxy_cache one;
            #proxy_cache_key sfs$request_uri$scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
    # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem; 
    # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

Notice the last if clause. That is where all the redirection takes place. Basically, you use only one server block and listen to all the traffic that comes through port 80 and 443. When someone hits port 80, the request is redirected to 443. Works well for me. Let me know if it does for you as well.

Ashish
  • 679
  • 7
  • 19
  • Thank you so much, I really appreciate it, it was much simpler. I've modified my server block and now get a 403 Forbidden error, `directory index of "/path/to/root/" is forbidden` do you have any idea why this happens? The other answers suggest removing the second `$uri` but that is already done. Thank you again. – Trilla Mar 06 '19 at 12:08
  • There could be multiple things going wrong there. You'd have to check for them one after the other. Thing like using index index.html index.htm index.php might help. Or having different user/permissions issue. Try this post if you haven't - https://stackoverflow.com/questions/19285355/nginx-403-error-directory-index-of-folder-is-forbidden – Ashish Mar 06 '19 at 22:27
  • Yes I've looked at that question but no luck so far, I've posted another question -https://stackoverflow.com/questions/55029556/403-forbidden-directory-index-of-path-to-files-is-forbidden-nginx?noredirect=1#comment96812117_55029556 but ended up breaking the file system structure when I started messing with changing permissions so I won't be doing that again, I will try using `index.html` but my app is a Python Django app so I would need to find out what the equivalent is. – Trilla Mar 07 '19 at 08:29
  • Also, shouldn't I uncomment all the location block? – Trilla Mar 07 '19 at 13:37