NGINX-WordPress无限重定向循环

时间:2019-04-09 17:19:46

标签: wordpress nginx dns redirect-loop

我是使用nginx的新手。到目前为止,我一直使用APACHE2作为托管引擎,因此,我的网站迁移存在问题

这是我的VHOST配置文件:

server {
        root /var/www/html/domain_com/web/;
        index index.php index.html index.htm;
        server_name domain.com www.domain.com;

        error_log /var/www/html/domain_com/log/error.log;
        access_log /var/www/html/domain_com/log/nginx-access.log;

        try_files $uri $uri/ /index.php?$args;

        location ~* \.php$ {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

                fastcgi_buffer_size 128k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
        }

        include /var/www/html/domain_com/web/nginx.conf; 

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.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
}

server {
    if ($host = www.domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name domain.com www.domain.com;
    return 404; # managed by Certbot
}

使用这样的配置,当我致电网站时,它会向我返回错误“太多重定向”,但是如果我将在第一个服务器块的顶部添加listen 80;,一切正常,但是certbot在为下一个域将显示有关该服务器名称已存在的警告,以及有关该警告已被忽略的信息。

我该怎么办才能解决此问题? :)

1 个答案:

答案 0 :(得分:0)

server {
    listen 80;
    server_name domain.com www.domain.com;
    rewrite ^ https://$server_name$request_uri permanent;
}

server {
        listen 443 ssl; # managed by Certbot
        root /var/www/html/domain_com/web/;
        index index.php index.html index.htm;
        server_name domain.com www.domain.com;

        error_log /var/www/html/domain_com/log/error.log;
        access_log /var/www/html/domain_com/log/nginx-access.log;

        try_files $uri $uri/ /index.php?$args;

        location ~* \.php$ {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

                fastcgi_buffer_size 128k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
        }

    include /var/www/html/domain_com/web/nginx.conf; 


    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.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


}