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

Nginx URL proxy to server

· 52CE 我爱测

I'm trying to redirect all traffic from www.example.com and example.com to old-www.website.com proxy all traffic from example.com/forum (and www) to forum server @ 192.168.100.100 I've tried the following config but it keeps retu…

问题原文

原帖链接 · f.farah · CC BY-SA 3.0

I'm trying to

  • redirect all traffic from www.example.com and example.com to old-www.website.com

  • proxy all traffic from example.com/forum (and www) to forum server @ 192.168.100.100

I've tried the following config but it keeps returning me to my old website When I remove the location / the forum is not loading and I'm getting 404's since the server is trying to load files @ example.com/ instead of example.com/forum

server {
    listen 80;
    server_name www.example.com example.com;

    location /forum/ {
        proxy_pass         http://192.168.100.100/;
        proxy_redirect     off;
        proxy_set_header   Host $host;
    }

    location / {
        return 302 $scheme://www.old-site.com;
    }
}

how can I fix this?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

The issue is the extra / in your proxy_pass statement. This causes nginx to use / as the URI with your forum always.

Try this:

location ~ /forum(/.+)? {
     proxy_pass http://192.168.100.100$1;
     proxy_redirect off;
     proxy_set_header Host $host;
}

Here I assume that you want the URL http://www.example.com/forum/something get proxied to http://192.168.100.100/something. Without the regular expression match and $1 in the proxy_pass, it would proxy to http://192.168.100.100/forum/something.

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

资料来源与适用范围

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

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

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