【发布时间】:2021-12-25 16:31:52
【问题描述】:
我的 gatsby 网站重定向到斜杠,然后再次重定向回非斜杠。
nginx 配置文件:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
return 301 https://www.$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name www.example.com;
location ~ /.well-known {
allow all;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* \.(?:html)$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location = /sw.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /page-data {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /static {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~* \.(?:js|css)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
error_page 404 /404.html;
}
当我打开带有斜杠的网站时,我看到重定向到相同的 URL,但没有斜杠:
example.com/hello-world/ -> example.com/hello-world
这是预期的行为。
但是当我打开没有斜杠的网站时,我看到重定向到斜杠,然后再次重定向到没有斜杠。
example.com/hello-world -> example.com/hello-world/ -> example.com/hello-world
这不应该发生。我的配置哪里出错了?非常感谢任何帮助。
【问题讨论】:
-
我在您的配置中看不到任何会导致重定向“预期行为”的内容。
/etc/nginx/conf.d或/etc/nginx/nginx.conf中是否还有其他可能导致重定向的 nginx 配置文件?也许你的 ssl sn-ps 文件中有一些东西?您确定它是服务器端3xx重定向,而不是基于您的hello-world内容的客户端重定向吗?尾部斜杠重定向通常是由于我在您的配置中没有看到的 nginxrewrite造成的。 -
与我 VM 上其他文件中的重定向无关。我在本地启动了我的网站,但看不到这种行为。在本地,它只是按预期从
hello-world/重定向到hello-world。 -
"在本地,它只是按预期从 hello-world/ 重定向到 hello-world。"为什么会这样?我在你的配置中没有看到这个。
-
我猜这就是 Gatsby.js 代表我所做的。
-
是的,好的。我没有看到您的 nginx 配置有任何问题(无论如何都与重定向有关)。我怀疑您的问题出在 gatsby.js 方面。您的帖子中没有足够的信息来帮助您。
标签: nginx webserver digital-ocean http-status-code-301