有一个我无法弄清楚的 NGINX 重定向循环

有一个我无法弄清楚的 NGINX 重定向循环

我在 nginx 服务器上运行此配置,并且遇到了重定向循环。背后的应用程序是 prestashop 1.7.xx

不幸的是,我不知道原因...有人可以解释一下吗?:)

显然,我已经更改了配置中的某些参数,但这应该没有问题。

nginx -t 没有给出错误。

我是 nginx 新手,我遵循以下指南:https://linuxize.com/post/how-to-install-prestashop-on-ubuntu/

# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.example.com example.com;

include snippets/letsencrypt.conf;
return 301 https://example.com$request_uri;
}

# Redirect WWW -> NON WWW

server {
listen 443 ssl http2;
server_name www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;

return 301 https://example.com$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

root /var/www/html/example.com;
index index.php;

# SSL parameters
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;

# log files
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

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

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location / {
    try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    expires max;
    log_not_found off;
}

}

答案1

在我看来,你的配置翻倍了,

  • 端口 80 部分;您正在 80 上监听 (www.)domain.com

  • 端口 443 首先,您正在监听 443 端口以连接 www.domain.com -- 在这里您必须使用 domain.com

  • 端口 443 其次,您正在监听 443 端口上的 domain.com -- 这里有您的负载 ---- 但为什么呢?在我看来,您让配置变得复杂却没有任何好处……?


在我看来,你应该尝试一下:

# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.example.com example.com;

include snippets/letsencrypt.conf;
return 301 https://example.com$request_uri;
#here you already tell the browser to use example instead of any other
}


server {
#anyway, if really www is still here we rewrite it permanently
if ($host = 'www.example.com' ) {
rewrite ^ https://example.com$uri permanent;
}

listen 443 ssl http2;
server_name example.com www.example.com;

root /var/www/html/example.com;
index index.php;

# SSL parameters
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;

# log files
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

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

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location / {
    try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
    expires max;
    log_not_found off;
}

} #i am not sure, if this must be removed

我希望我能帮助你

相关内容