Redirect 如何使nginx代理停止重定向到root?

Redirect 如何使nginx代理停止重定向到root?,redirect,nginx,proxy,Redirect,Nginx,Proxy,我有一个节点应用程序在nginx的子目录中作为代理运行。我在重定向到应用程序本身的不同部分时遇到了问题。它们总是重定向到根目录,而不是代理的子目录 示例: 如果我的应用程序代理位于https://example.com/myApp/并重定向到/admin/我希望页面重定向到https://example.com/myApp/admin/非https://example.com/admin/ 以下是我的配置的相关部分: location /myApp { rewrite /myApp

我有一个节点应用程序在nginx的子目录中作为代理运行。我在重定向到应用程序本身的不同部分时遇到了问题。它们总是重定向到根目录,而不是代理的子目录

示例:

如果我的应用程序代理位于
https://example.com/myApp/
并重定向到
/admin/
我希望页面重定向到
https://example.com/myApp/admin/
https://example.com/admin/


以下是我的配置的相关部分:

  location /myApp {
    rewrite /myApp(.*) $1 break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:3030;
    proxy_redirect http://localhost:3030/ /myApp/;
  }
我还尝试将
proxy\u重定向
设置为
(^|^http://localhost:3030)//myApp/


以下是此域的完整配置文件:

upstream php-handler {
  #server 127.0.0.1:9000;
  server unix:/var/run/php5-fpm.sock;
}

## HTTP
server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  server_name example.com www.example.com;
  return 301 https://$server_name$request_uri;
}

## HTTPS
server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /etc/ssl/www.example.com.crt;
  ssl_certificate_key /etc/ssl/www.example.com.key;

  add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
  add_header X-Content-Type-Options nosniff;
  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";

  root /var/www/example.com/;
  index index.html index.php;

  client_max_body_size 500M;
  fastcgi_buffers 64 4k;
  gzip on;

  # Website
  location / {
    try_files $uri $uri/ =404;

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
    }

  }

  # My App
  location /myApp {
    rewrite /myApp(.*) $1 break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:3030;
    proxy_redirect http://localhost:3030/ /myApp/;

  }

在我看来,您应该在服务器块中尝试listen指令,将其定义为默认值(源:):

以下声明表示为无效的服务器名称(源:)(因此它不会与任何有效域相交):

要解决此问题,请确保您有一个带有侦听(端口)和服务器名称(虚拟主机)的服务器块-当您在nginx中进行虚拟主机名匹配时,当您从“更精确到更不精确”时,将首先触发此块:

Listen 80 

Server_name your-domain-name.com;

好吧,我自己想出来的

tl;医生:

这起到了作用:

proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;

第一种选择: 我猜这是因为应用程序正在重定向到
/admin/
而不是
http://localhost:3030/admin/
。然而,这并没有提供我所期望的灵活性


第二种选择: 使用RegExp 所以我最初尝试使用regex时犯了几个错误,如下所示:

proxy_redirect (^|^http://localhost:3030)/ /myApp/;
proxy_rewrite ~/(a|b)/(.*)$ /myApp/$2;
proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;
首先,如果要使用regex,则需要以
~
开头以区分大小写,或以
~*
开头以区分大小写

其次,当您使用regex时,它显然会替换完整路径,而不仅仅是匹配的部分。例如,如果要像这样设置代理\u重定向:

proxy_redirect ~/(a|b)/ /myApp/;
如果随后重定向到
/a/some subdirectory/
,重写将导致
/myApp/
而不是
/myApp/some subdirectory/
。要完成此操作,您需要捕获路径的结尾并将其插入到重写的结尾,如下所示:

proxy_redirect (^|^http://localhost:3030)/ /myApp/;
proxy_rewrite ~/(a|b)/(.*)$ /myApp/$2;
proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;
注意:我在文档中找不到有关此行为的任何信息。我只是根据我自己的尝试和错误的结果


因此,我的最终代理_重定向如下所示:

proxy_redirect (^|^http://localhost:3030)/ /myApp/;
proxy_rewrite ~/(a|b)/(.*)$ /myApp/$2;
proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;

我不太明白你想说什么。我的位置块嵌套在域的服务器块中。你是说不应该这样吗?我将添加域的其余配置,以防止任何混淆。