3

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?

  • Please use example.com as an example domain. – xenteros Jun 24 '16 at 7:28
8

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

}
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;
}
  • 1
    In your answer, you added the X-Host directive, not proxy_redirect off. Is that what you meant to do? – Tyler Collier Aug 17 '14 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.