我试图使用不同的页面为PC和modile,但添加一些如果条件和重写模式后,我遇到了一些新的错误后请求的APIS .这里是我的nginx配置
server {
listen 80 ;
server_name some;
gzip on;
gzip_types text/javascript;
location ^~ /v1 {
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://market;
}
if ($is_pc) {
rewrite ^/(?!pc/)(?!.*\.(js|css))(.*)$ /pc/dist/$1 last;
}
location / {
root /etc/nginx/market/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /pc/ {
root /etc/nginx/market/;
index /pc/dist/index.html;
try_files $uri $uri/ /pc/dist/index.html;
}
location ~ ^/assets/.*\.(js|css)$ {
rewrite ^(.*)$ https://some.cdn/khd$1 permanent;
}
location ~ ^/pc/dist/assets/(.*\.(js|css))$ {
rewrite ^/pc/dist/assets/(.*)$ https://some.cdn/khd/assets/$1 permanent;
}
字符串
}
当我打开页面,它得到一个错误,当发布到/v1/productList与错误http代码405我不知道如何nginx考虑这个后请求作为一个请求静态文件.和想法如何解决这个问题?或任何更好的方法来做到这一点?
1条答案
按热度按时间qyzbxkaa1#
rewrite module(包括
if
,rewrite
和return
)将在server
上下文中评估其指令**,然后**Nginx选择location
块来处理请求。因此,您的
if (...) { rewrite...last; }
可以将通常与location ^~ /v1
匹配的URL更改为不匹配的URL。如果将
if (...) { rewrite...last; }
块移动到location /
中,Nginx将首先选择一个location
块来处理请求,这意味着匹配location ^~ /v1
的URL将不再被该代码重写。请注意,
location
上下文中的if
块具有additional restrictions,但是,rewrite...last
可以安全使用。