Nginx 301 https上的永久重定向,但http工作正常

Nginx 301 https上的永久重定向,但http工作正常,nginx,redirect,http-status-code-301,lets-encrypt,Nginx,Redirect,Http Status Code 301,Lets Encrypt,当我跑的时候 curl -I http://myapp.com/ curl -I https://myapp.com/ http返回 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

当我跑的时候

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

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
https
返回

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
我显然无法访问域,我得到了错误
ERR\u TOO\u MANY\u REDIRECTS
,因为Let's Encrypt设置为将任何http请求重定向到https

我看到两个选项,停用
https
并通过
http
访问站点,或者找出
https
上发生
301永久重定向的原因

最初,我去掉了
http
上的
301永久重定向

return 301 https://$server_name$request_uri;
我的nginx配置文件

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;
}
}

我急需帮助,请

您可以采用这种替代方法。我发现处理重定向比较简单

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;
    }
}

注意最后一个if子句。这就是所有重定向发生的地方。基本上,您只使用一个服务器块,并侦听通过端口80和443的所有通信量。当有人点击端口80时,请求被重定向到443。对我来说很好。如果它对您也有帮助,请告诉我。

非常感谢,我真的很感激,它简单多了。我修改了我的服务器块,现在得到一个403禁止错误,
目录索引“/path/to/root/”被禁止
你知道为什么会发生这种情况吗?其他答案建议删除第二个
$uri
,但这已经完成了。再次感谢你。那里可能会出很多问题。你必须一个接一个地检查它们。使用index.html index.htm index.php之类的东西可能会有所帮助。或具有不同的用户/权限问题。如果您没有-是的,我已经看了这个问题,但到目前为止运气不好,我发布了另一个问题-但是在我开始更改权限时破坏了文件系统结构,所以我不会再这样做了,我将尝试使用
index.html
,但我的应用程序是一个Python Django应用程序,因此我需要找出等价物。另外,我是否应该取消对所有位置块的注释?