5

I have a.b domain (for example) and want to serve some github pages (username.github.io/project) in a.b/c. It means that I also want to keep my browser url to a.b/c and showing contents of username.github.io/project.

I have following settings in nginx module

location /c {       
    proxy_pass http://username.github.io/project;
    proxy_redirect http://username.github.io http://a.b;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
}

if I change proxy_set_header Host $http_host to proxy_set_header Host $proxy_host or $host, it just redirect to http://username.github.io/project which is not I meant to. How can I do?

Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
Jongsu Liam Kim
  • 161
  • 1
  • 1
  • 6

2 Answers2

10

Simply send the right Host header to your proxy target by removing the proxy_set_header Host $http_host line.

If a.b is configured as a server name in your server block then you don't even need proxy_redirect directive if you use a trailing slash in your location prefix and in your proxy_pass target as explained in the documentation :

Syntax:  proxy_redirect default;
         proxy_redirect off;
         proxy_redirect redirect replacement;
Default: proxy_redirect default;
Context: http, server, location

[ ...]

The default replacement specified by the default parameter uses the parameters of the location and proxy_pass directives. Hence, the two configurations below are equivalent:

location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect default;
}

location /one/ {
    proxy_pass     http://upstream:port/two/;
    proxy_redirect http://upstream:port/two/ /one/;
}

[ ....]

So, this should do it :

server {

    server_name a.b;

    location /c/ {       
        proxy_pass http://username.github.io/project/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;
    }

}
CGK
  • 105
  • 5
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
0

use

proxy_redirect off;

So your settings will be

location /c {

    proxy_pass http://username.github.io/project;
    proxy_redirect http://username.github.io;
    proxy_set_header Host username.github.io;
    proxy_set_header X-Host username.github.io;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
}
user1876508
  • 103
  • 3
mohit
  • 101
  • 1
  • 1