Nginx是否可以插入一个缺少的“/&引用;“或丢失”/bla/";在URL中?

Nginx是否可以插入一个缺少的“/&引用;“或丢失”/bla/";在URL中?,nginx,Nginx,查看Nginx的error.log文件,我们可以看到请求是两种不正确模式之一: ->应该是 ->应该是 当前的Nginx配置如下所示: server { listen 8080; location /app-context/ { proxy_redirect off; proxy_set_header Host $host; proxy_pass http://localhost:8888/app-context/; } }

查看Nginx的error.log文件,我们可以看到请求是两种不正确模式之一:

  • ->应该是
  • ->应该是
当前的Nginx配置如下所示:

server {

  listen        8080;

  location /app-context/ {

    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_pass http://localhost:8888/app-context/;
  }
}
挑战在于插入缺少的/after应用程序上下文(对于第一个错误的URL)或缺少的/moduleB/(对于第二个错误的URL)。看起来try_文件不支持这一点,我还没有找到重写的方法


Nginx是否有办法重写这两种用例的URL?特别是,我不希望事先知道所有模块或控制器的名称。有很多,因此在重写规则中对它们进行“硬编码”将是非常繁重的。

这些应该可以处理您的示例:

    location /app-context {
        rewrite ^(/app-contextmoduleA)/(.*)$ /app-context/moduleA/$2 permanent;
        rewrite ^(/app-contextcontroller2) /app-context/moduleB/controller2 permanent;
        ...
    }

查看ngx_http_重写_模块了解更多信息。

这些应该可以处理您的示例案例:

    location /app-context {
        rewrite ^(/app-contextmoduleA)/(.*)$ /app-context/moduleA/$2 permanent;
        rewrite ^(/app-contextcontroller2) /app-context/moduleB/controller2 permanent;
        ...
    }

查看ngx_http_rewrite_模块了解更多信息。

通过查阅大量在线资源和反复试验,我找到了一个足够好的解决方案:

location /app-context {   
    location ~ (moduleA|moduleB) {
      # inserts a forward slash after app-context if not there, 
      # e.g. /app-contextmoduleA/foo/bar to /app-context/moduleA/foo/bar
      rewrite ^(/app-context(?!/))(.*) $1/$2 break;
      try_files $uri @proxy;
    }
    # inserts /defaultModule/ after app-context
    # e.g. /app-context/controller1 to /app-context/defaultModule/controller1
    rewrite ^(/app-context(?!/defaultModule/))(.*) $1/defaultModule/$2 break;
    try_files $uri @proxy;
}

location @proxy {
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_pass http://localhost:8888;
}

通过查阅大量在线资源和反复试验,我找到了一个足够好的解决方案:

location /app-context {   
    location ~ (moduleA|moduleB) {
      # inserts a forward slash after app-context if not there, 
      # e.g. /app-contextmoduleA/foo/bar to /app-context/moduleA/foo/bar
      rewrite ^(/app-context(?!/))(.*) $1/$2 break;
      try_files $uri @proxy;
    }
    # inserts /defaultModule/ after app-context
    # e.g. /app-context/controller1 to /app-context/defaultModule/controller1
    rewrite ^(/app-context(?!/defaultModule/))(.*) $1/defaultModule/$2 break;
    try_files $uri @proxy;
}

location @proxy {
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_pass http://localhost:8888;
}

例如,
rewrite^(/appcontext)([^/].+)$$1/$2 lastrewrite^(/appcontext)([^/].+)$$1/$2 last有许多模块和控制器,因此我需要用“任意字符”替换模块A和控制器2。例如,使用
\/app context(?!\/moduleA)(*)
我可以找到需要修复的模式。有许多模块和控制器,因此我需要用“任意字符”替换模块A和控制器2例如,使用
\/appcontext(?!\/moduleA)(.*)
我可以找到需要修复的模式。