为什么nginx在添加一些重写和try_file patton后处理post请求错误?

wpcxdonn  于 5个月前  发布在  Nginx
关注(0)|答案(1)|浏览(45)

我试图使用不同的页面为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考虑这个后请求作为一个请求静态文件.和想法如何解决这个问题?或任何更好的方法来做到这一点?

qyzbxkaa

qyzbxkaa1#

rewrite module(包括ifrewritereturn)将在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可以安全使用。

相关问题