nginx:从http到https的无限重定向,反之亦然

时间:2019-12-23 11:06:19

标签: nginx

我在网站上遇到无限重定向的问题。 Nginx成功地从http重定向到https,但是由于某种原因,它做了相反的事情,从而导致无限循环。我的配置有什么问题?

erver {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name devcore.pw www.devcore.pw;

    return 301 https://devcore.pw$request_uri;
}

server {
    client_max_body_size 20M;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;
        server_name www.devcore.pw devcore.pw; # managed by Certbot

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

    # pass PHP scripts to FastCGI server

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        # With php-cgi (or other tcp sockets):
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on; 
    }

     #deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny all;
    }

        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
    ssl on;
        ssl_certificate /etc/letsencrypt/live/devcore.pw/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/devcore.pw/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
}```

1 个答案:

答案 0 :(得分:0)

此配置存在主要问题。您在重定向行后关闭了服务器块 因此,您必须对其进行修复,请尝试:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    server_name devcore.pw www.devcore.pw;
    return 301 https://devcore.pw$request_uri;
    client_max_body_size 20M;
    root /var/www/html;
    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

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

    # pass PHP scripts to FastCGI server

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        # With php-cgi (or other tcp sockets):
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on; 
    }

     #deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny all;
    }


        ssl on;
        ssl_certificate /etc/letsencrypt/live/devcore.pw/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/devcore.pw/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
}