How to redirect nginx proxy server to root?

I have a node application running as a proxy in nginx in a subdirectory. I'm having redirection issues that point to another part of the application itself. They are always redirected to the root directory instead of the proxy subdirectory.

Example:

If my application proxy is in https://example.com/myApp/and redirected to /admin/, I want the page to redirect to https://example.com/myApp/admin/not https://example.com/admin/.


Here is the relevant section of my configuration:

  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/;
  }

I also tried installing proxy_redirecton(^|^http://localhost:3030)/ /myApp/;


Here is the complete configuration file for this domain:

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/;

  }
+4
source share
2 answers

, .

TL;DR:

:

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

:

proxy_redirect / /myApp/;

, , /admin/ not http://localhost:3030/admin/. , , , .


:

RegExp

, , :

proxy_redirect (^|^http://localhost:3030)/ /myApp/;

, , ~ ~* .

-, , , -, , . , proxy_redirect :

proxy_redirect ~/(a|b)/ /myApp/;

/a/some-subdirectory/, /myApp/ /myApp/some-subdirectory/. , :

proxy_rewrite ~/(a|b)/(.*)$ /myApp/$2;

. . .


, proxy_redirect :

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

, listen , (source: http://nginx.org/en/docs/http/request_processing.html)

listen       80  

default_server;

(: http://nginx.org/en/docs/http/server_names.html) ( - )

Server_name _; 

, , () _ ( ) - , " ", nginx:

Listen 80 

Server_name your-domain-name.com;
-2

All Articles