大网数据广告活动
服务器运维问答

Use nginx reverse proxy for redirection

· 52CE 我爱测

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 f…

问题原文

原帖链接 · Jongsu Liam Kim · CC BY-SA 3.0

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 · CC BY-SA 4.0

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

}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-02-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。