我發現了幾種不同的方法來重定向 NGINX 中的路徑。問題是我有一個子模塊,其中包含/foo/
子目錄的路徑,例如/foo/wisdom/script.js
.
我最接近的是:
rewrite ^(/foo/)(.*)$ /bar/$2 permanent;
js 和 css 檔案拋出 404。
顯然解決方法是使用和別名,我發現了這個:
location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)$ {
alias /bar/;
}
但是通配符不正確匹配,我不知道如何修復它。
編輯:添加 nginx.conf 以獲取更多資訊:
worker_processes auto;
pid /run/nginx.pid;
error_log /dev/stdout info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
keepalive_timeout 70;
access_log /dev/stdout;
error_page 404 /404.php;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
server_tokens off;
if ($request_method !~ ^(GET|POST|PUT|DELETE)$ ) {
return 405;
}
## THIS WORKS BUT NESTED JS AND CSS RETURN 404
#rewrite ^(/foo/)(.*)$ /bar/$2 permanent;
## THIS DOESN'T MATCH
#location ~* ^(/foo/)(.*)$ {
# alias /bar/$2;
#}
## THIS DOESN'T MATCH
#location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)${
# alias /bar/$1;
#}
location /api/query1 {
proxy_pass http://api:1234/api/query1;
}
location /api/query2 {
proxy_pass http://api:1234/api/query2;
}
location /api/query3 {
proxy_pass http://api:1234/api/query3;
}
location /api/query4 {
proxy_pass http://api:1234/api/query4;
}
location /api/query5 {
proxy_pass http://api:1234/api/query5;
}
location /api/query6 {
proxy_pass http://api:1234/api/query6;
}
location /api/query7 {
proxy_pass http://api:1234/api/query7;
}
location /api/query8 {
proxy_pass http://api:1234/api/query8;
}
location /api/query9 {
proxy_pass http://api:1234/api/query9;
}
location /api/query10 {
proxy_pass http://api:1234/api/query10;
}
location /api/query11 {
proxy_pass http://api:1234/api/query11;
}
location /bar {
proxy_pass http://api:5000/foo;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
expires 30d;
tcp_nodelay off;
access_log off;
}
location / {
if ($request_uri ~ ^/(.*)\.php) {
return 302 /$1;
}
autoindex off;
index index.php;
root /var/www;
try_files $uri $uri.html $uri.php $uri/ =404;
#rewrite ^/assets/([a-z\-] )-([a-z0-9] ).(css|js|png|webp) /assets/$1.$3;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
root /var/www;
}
location ~ \.css {
root /var/www;
add_header Content-Type text/css;
}
location ~ \.js {
root /var/www;
add_header Content-Type application/x-javascript;
}
location = /404.php {
root /var/www;
}
}
}
編輯2:這也部分有效:
location ~ ^/foo/(.*) {
return 301 /bar/$1;
}
但是,當檢查錯誤日志時,我可以看到 nginx 正在尋找 css 和 js 檔案,/var/www/bar/...
而不是在我上面的配置http://localhost/bar/...
中https://api:5000/foo/...
代理。
uj5u.com熱心網友回復:
當您在正則運算式位置使用alias
指令時,您需要指定匹配檔案的完整物理路徑,例如
location ~* ^/foo/(.*\.(?:jpg|png|css|js|appcache|xml|ogg|m4a))$ {
alias /foo/bar/$1;
}
uj5u.com熱心網友回復:
所以重寫是正確的解決方案。我的代理不正確。解決方法如下。
改寫:
rewrite ^(/foo/)(.*)$ /bar/$2 permanent;
代理:
location ^~ /bar {
proxy_pass http://api:5000/foo;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
記下^~
位置標簽之后的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/470870.html
標籤:http nginx 重定向 url重写 http-status-code-404
上一篇:HTTP重定向不呈現新頁面