nginx重写 /重定向 /别名通配符全卡
我找到了几种不同的方法来重定向nginx中的路径。问题是我有一个子模块,其中包含/foo/
带有/foo/foo/wisdom/script.js.js
之类的subdirs的路径。
我来的最接近的是: 重写 ^(/foo /)(.** fory/bar/$ 2永久;
为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在/var/var/www/bar /...
而不是http:// http:// localhost // bar /...
https:// api:5000/foo/...
在我上面的配置中使用。
I've found a few different ways to redirect a path in NGINX. The issue is I have a submodule that contains paths to /foo/
with subdirs like /foo/wisdom/script.js
.
The closest I've come is:rewrite ^(/foo/)(.*)$ /bar/$2 permanent;
Which throws 404 for js and css files.
Apparently the fix is to use and alias instead, I found this:
location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)$ {
alias /bar/;
}
However the wildcard doesn't match properly and I'm not sure how to fix it.
EDIT:
Adding nginx.conf for more info:
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;
}
}
}
EDIT 2:
This also partially works:
location ~ ^/foo/(.*) {
return 301 /bar/$1;
}
However when checking error logs I can see nginx is looking for css and js files in /var/www/bar/...
instead of http://localhost/bar/...
which is https://api:5000/foo/...
proxied in my config above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,重写是正确的解决方案。我的代理不正确。解决方案如下。
重写:
代理:
在位置标签后记下
^〜
。So the rewrite was the correct solution. My proxy was incorrect. Solution as follows.
Rewrite:
Proxy:
Take note of the
^~
after the location tag.