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

Nginx redirecting proxy_pass server to http://server/index.html

· 52CE 我爱测

I have the following config: server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _…

问题原文

原帖链接 · Matt · CC BY-SA 3.0

I have the following config:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;
    location / {
    index index.html;
    }


    server_name command_asdf
    location  /asdf/ {
    proxy_pass http://127.0.0.1:3001/;
    }

When I go to http://server/asdf/index.html everything works fine.

However, if I go to http ://server/asdf/ or http ://server/asdf, they both redirect me to http ://server/index.html instead of http ://server/asdf/index.html

I've spent hours trying to figure out how to instead get it to redirect to the index page on the sub-directory, but sadly had no success.

I have a number of servers I need to use proxy_pass on, however above is the simplest config I can show.

Thanks!

已采纳解答

原帖链接 · Matt · CC BY-SA 3.0

Weeks later..Have managed to fix my problem. The solution is:

   proxy_redirect    / $request_uri;

So when a user accesses foo.com/bar/ it would redirect to foo.com/index.html

This was happening in the 302 redirect the proxy_pass server was issuing.

The above line added to my config will redirect foo.com/bar/ correctly to http://foo.com/bar/ which then loads the index.html page.

Full config:

    server_name bar;
    location  /bar/ {
    proxy_pass http://127.0.0.1:3001/;
    proxy_redirect    / $request_uri;
    }

For reference, $request_uri is equal to the location and sub-directory i.e. foo.com/bar/

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

资料来源与适用范围

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

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

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