Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an nginx proxy stop redirecting to root?

I have a node app running as a proxy on nginx in a sub-directory. I'm having trouble with redirects that point to a different part of the app itself. They always redirect to the root instead of the proxy's sub-directory.

Example:

If my app proxy is at https://example.com/myApp/ and it redirects 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 config:

  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 setting proxy_redirect to (^|^http://localhost:3030)/ /myApp/;


Here is the full config 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/;

  }
like image 571
Hal Carleton Avatar asked Nov 09 '22 15:11

Hal Carleton


1 Answers

Alright, I figured it out on my own.

tl;dr:

This worked:

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

First option:

proxy_redirect / /myApp/;

I'm guessing this works because the app is redirecting to /admin/ not http://localhost:3030/admin/. This, however, does not provide the flexibility I'm looking for.


Second Option:

Use RegExp

So I made a couple mistakes originally when I tried using regex like this:

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

First, if you're going to use regex you need to start it with ~ for case sensitive or ~* for case insensitive.

Secondly, when you use regex it apparently replaces the full path instead of just the matched portion. For instance if you were to set a proxy_redirect like this:

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

If you then redirect to /a/some-subdirectory/ the rewrite will result in /myApp/ not /myApp/some-subdirectory/. To make this work you need to capture the end of the path and insert that onto the end of the rewrite like this:

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

Note: I can't find any information on this behavior in the docs. I am just basing this on the results of my own trial and error.


So my final proxy_redirect looks like this:

proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;
like image 177
Hal Carleton Avatar answered Nov 15 '22 07:11

Hal Carleton