我已經有兩個PHP檔案,Web服務器,index.php并且controller.php,它處理了所有的非/請求后者p(換頁)paramater,如
/controller.php?p=some_page
以下 nginx 配置運行良好:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
但是,我現在想要清理 URL,并且想要/去index.php和/some_page去/controller.php?p=some_page.
這是我正在嘗試的新配置:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
# this is new, and makes index.php handle /
location / {
index index.php;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# this is new, but doesn't work.
location @rewrites {
if ($uri ~* ^/([0-9a-z_] )$) {
set $page_to_view "/controller.php?p=$1";
rewrite ^/([0-9a-z_] )$ $scheme://$http_host/controller.php?p=$1 last;
}
}
}
瀏覽器地址欄顯示:
- http://localhost:8080/some_page (initial request)
- http://localhost:8080/controller.php?p=some_page (first redirect one second later)
- https://localhost/some_page/ (final redirect another second later)
因此,它最終出現在一個沒有原始埠的 URL 上,帶有尾部斜杠,并使用 scheme https。
我能做些什么來修復它?
PS 如果尾部斜杠無關緊要(即localhost:8080/some_page并localhost:8080/some_page/顯示相同的內容),那將是一個獎勵。
PS 埠 8080 只是我通過 Docker 在本地測驗,它將容器 80 映射到主機 8080。
更新:
我已經實施了理查德的答案,并嘗試了建議的curl:
$ curl -I http://localhost:8080/some_page
HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.4
Date: Thu, 09 Dec 2021 15:07:11 GMT
Content-Type: text/html
Content-Length: 169
Location: http://localhost/some_page/
Connection: keep-alive
注意缺少 8080 埠。
更新 2:
使用 nginx 指令absolute_redirect off;,我得到了這個:
$ curl -I http://localhost:8080/some_page
HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.4
Date: Thu, 09 Dec 2021 16:01:31 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: /some_page/
$ curl -I http://localhost:8080/some_page/
HTTP/1.1 404 Not Found
Server: nginx/1.21.4
Date: Thu, 09 Dec 2021 16:01:34 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
因此,問題仍然是請求如何提供/some_page/回應/controller.php?p=some_page
uj5u.com熱心網友回復:
命名位置(例如location @rewrites)通常從try_files陳述句的最后一個元素呼叫。
例如:
location / {
index index.php;
try_files $uri $uri/ @rewrites;
}
在您的location @rewrites塊中,該if陳述句是不必要的,該set變數未使用,該rewrite陳述句將引發外部重定向。
嘗試:
location @rewrites {
rewrite ^/([0-9a-z_] )$ /controller.php?p=$1 last;
}
uj5u.com熱心網友回復:
我已經找到了一個可以與原始鏈接一起使用的配置,還有我想要的漂亮的 URL。
server {
listen 80;
index index.php;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location = / {
index index.php;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* {
rewrite ^/([0-9a-z_] )/?$ /controller.php?p=$1 last;
}
}
關鍵似乎是不使用命名位置進行重寫,而只是將“其他所有內容”與location ~*請求不是針對/PHP 檔案的情況相匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/378412.html
