简体   繁体   English

Nginx将HTTP重写为HTTPS表示重定向循环?

[英]Nginx rewrite HTTP to HTTPS says redirect loop?

I am trying to redirect all HTTP traffic to HTTPS,so when i got to www.domain.com it goes to https://www.domain.com . 我正在尝试将所有HTTP通信重定向到HTTPS,因此当我转到www.domain.com时,它将转到https://www.domain.com This is my current .conf file - 这是我当前的.conf文件-

server {

listen 80;
#listen [::]:80;
server_name www.domain.net;
rewrite ^(.*) https://www.domain.net$1 permanent;
index index.html index.htm index.php default.html default.htm default.php;
root  /home/wwwroot/www.domain.net;

include other.conf;
#error_page   404   /404.html;
location ~ [^/]\.php(/|$)
    {
        # comment try_files $uri =404; to enable pathinfo
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        #include pathinfo.conf;
    }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

access_log  /home/wwwlogs/www.domain.net.log  access; }

It always returns a 'redirect loop' error for my domain, I have tried many different configurations but I always have this same problem. 它总是为我的域返回“重定向循环”错误,我尝试了许多不同的配置,但是我始终遇到同样的问题。 (I appreciate my SSL is not configured as it should be but it still works) (我很欣赏我的SSL没有进行应有的配置,但仍然可以使用)

If someone could help me to get it working I would be grateful. 如果有人可以帮助我使它正常工作,我将不胜感激。

You need put the ssl configuration inside the server: 您需要将ssl配置放入服务器中:

listen 443 ssl default; 监听443 SSL默认值;
ssl on ssl_certificate << put your .crt >>; ssl_certificate上的ssl <<将您的.crt >>;
ssl_certificate_key << put your .key >>; ssl_certificate_key <<把您的.key >>;

# force https-redirects #强制https-redirects
if ($scheme = http) { return 301 https://$server_name$request_uri; 如果($ scheme = http){返回301 https:// $ server_name $ request_uri; } }

Do you put on your file the server to listen the port 443? 您是否将服务器上的文件放在端口443上监听?

Because if you don't configurate your nginx to listen the port 443, your server will redirect from 80 to https. 因为如果您不配置nginx来监听端口443,则服务器将从80重定向到https。 And from https to http (80) - this cause your loop. 从https到http(80)-这会导致循环。

To build your redirect you need only this: 要建立重定向,您仅需要以下步骤:

server {
    listen 80 default;
    server_name <<your domain>>;
    return 301 https://<<your domain>>$request_uri;}

And use the same configuration that you did on your 443 ssl but make those changes: 并使用与您在443 SSL上所做的配置相同的配置,但是要进行以下更改:

listen 443 ssl;
ssl_certificate <<your .crt>>;
ssl_certificate_key <<your .key>>;
ssl_client_certificate <<your client certificate .crt>>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM