local
local

Reputation: 437

Nginx reverse proxy doesn't work as expected

I have two domains on different nginx(1.15.0) servers (server1 example.com and server2 example.net). I've tried to set up server2 as a reverse proxy with ngx_http_substitutions_filter_module but it doesn't work as expected. Due to my config, subs_filter directive should replace example.com to example.net but when I type example.net in browser it redirects me to example.com.

nginx.conf

http {
 //other settings

 .....

 include /etc/nginx/sites-enabled/*;

 upstream example.com {
   server example.com;
 }

}

example.net.conf

server {
 listen 80;
 server_name example.net www.example.net;
 rewrite ^/(.*)$ https://example.net/$1 permanent;
}

server {
  listen 443 ssl; 
  server_name example.net;

  ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem; 
  ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem; 
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

  location / {
    root html;
    try_files $uri @example.com;
  }

  location @example.com {
    include replace.conf;
    proxy_pass http://example.com;

    proxy_cookie_domain example.com example.net;

    proxy_set_header Accept-Encoding "";
    proxy_set_header Host example.com;
    proxy_redirect http://example.com http://example.net;

  }
}

replace.conf

# replace direct links
subs_filter "www.example.com" "example.net" gi;
subs_filter "example.com" "example.net" gi;

Seems like nginx ignores subs_filter directive. Could someone explain me how can I replace uri properly using ngx_http_substitutions_filter_module? Thank you for advice!

Upvotes: 0

Views: 924

Answers (1)

local
local

Reputation: 437

Problem solved. First of all I removed upstream from nginx.conf:

upstream example.com {
  server example.com;
}

Then I changed following lines in example.net.conf:

  location / {
   ...
   try_files $uri @static;
  }

  location @static {
   ...
   proxy_pass https://example.com;

   ...

   ...
   ...
   proxy_redirect https://example.com https://example.net;
 }

All works fine except login form. Firefox works correctly but Chrome returns error 422. I suppose this is because the login form works on javascript. Anyway thanks to all!

Upvotes: 0

Related Questions