該賞金過期7天。回答這個問題有資格獲得 50聲望獎勵。 Robin Wieruch希望更多地關注這個問題。
我的網站使用以下 nginx 組態檔:
server {
listen 80;
listen [::]:80;
server_name mywebsite.de www.mywebsite.de;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-mywebsite.de.conf;
include snippets/ssl-params.conf;
server_name mywebsite.de;
return 301 https://www.$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-mywebsite.de.conf;
include snippets/ssl-params.conf;
root /var/www/mywebsite.de/html;
index index.html index.htm index.nginx-debian.html;
server_name www.mywebsite.de;
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 但沒有斜杠:
mywebsite.de/hello-world/ -> mywebsite.de/hello-world
這是預期的行為。
但是,當我打開沒有斜杠的網站時,我看到重定向到斜杠,然后再次重定向到沒有斜杠。
mywebsite.de/hello-world -> mywebsite.de/hello-world/ -> mywebsite.de/hello-world
這不應該發生。我的配置哪里出錯了?很感謝任何形式的幫助。
uj5u.com熱心網友回復:
nginx
Nginx 關于斜杠的行為記錄在此處。
如果位置由以斜杠字符結尾的前綴字串定義,并且請求由 proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass、memcached_pa??ss 或 grpc_pass 之一處理,則執行特殊處理。為回應 URI 等于此字串但沒有尾部斜杠的請求,帶有代碼 301 的永久重定向將回傳到附加斜杠的請求 URI。如果不需要,可以像這樣定義 URI 和位置的精確匹配:
location /user/ { proxy_pass http://user.example.com; } location = /user { proxy_pass http://login.example.com; }
這不適用于您,因為您沒有使用 CGI 或上游應用服務器……您只是提供檔案(如果我理解正確的話)。
我不是蓋茨比專家,而是來自您評論中的鏈接......
Gatsby 使用斜杠效果更好、更流暢、更干凈(等)
有各種各樣的 nginx 配置從那些在 Gatsby 上成功使用它們的人那里傳來。這里有一個例子。
與您最相關的部分是...
重定向不包含.或?AND/的路徑不以相同的路徑結束,但以尾隨/.
例如重定向/foo/bar到/foo/bar/但不要亂用/foo/bar.extor/fo.o/bar或/foo/bar?hello。
rewrite ^([^.\?]*[^/])$ $1/ permanent
嘗試按照以下優先順序提供所請求的 URI 來解釋它:
- 原樣(檔案路徑)
- 作為目錄
- 作為包含
index.html檔案的目錄 - 如果以上均無效,則最終 404 錯誤
try_files $uri $uri/ $uri/index.html =404;
蓋茨比
From the gatsby issue link you posted they say the following:
Explicitly use the trailing slash in all usages of the
<Link>component and/or invocations ofnavigate()
and
Install
gatsby-plugin-force-trailing-slashes. Even though Gatsby generates the static html pages within their own named directories by default, this plugin forces the path value for each page to end in a / - critical for configuring the core Gatsby @reach/router at build time.
and then finally rebuild the gatsby site since some of this config only has impact at build time.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/357688.html
標籤:nginx 网络服务器 数字海洋 http-status-code-301
