Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为项目后端配置反向代理时,静态文件和 API 请求无法正确转发 #520

Closed
Yi-Xio opened this issue Aug 21, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@Yi-Xio
Copy link

Yi-Xio commented Aug 21, 2024

Is your feature request related to a problem? Please describe.
我希望配置通过域名(如 https://example.com/nginxui )访问服务,我为此配置了反向代理

location /nginxui {
        proxy_pass http://127.0.0.1:9000/;
    }

但这样做的话,许多静态文件和 API 请求无法正确转发到后端服务器。这导致呈现空白页,功能无法正常使用。
我也尝试将静态文件的 /assets/ 路径 和 /api/ 路径配置转发至 http://127.0.0.1:9000 的对应路径,但我觉得这样可能存在某些潜在的问题,且似乎有些“不优雅”。
我查阅了项目文档,并没有找到相关说明。如果该功能已实现,烦请告知如何操作。

Describe the solution you'd like
我希望项目能够支持通过自定义子路径进行反向代理访问。例如,在 Nginx 中配置 /nginxui 作为子路径,能让所有相关请求(包括静态文件和 API 请求)正确转发到后端。

Describe alternatives you've considered
目前我尝试过使用不同的 Nginx 反向代理配置,然而问题依然存在。我认为项目需要内置对自定义子路径的支持,或者提供一个更清晰的指导来帮助用户解决这一问题。

Additional context
Nginx-UI 是非常优秀的项目,感谢作者大大的付出!

@Yi-Xio Yi-Xio added the enhancement New feature or request label Aug 21, 2024
@0xJacky
Copy link
Owner

0xJacky commented Aug 27, 2024

我们之前针对这种情况做过调整了 #68

只需要改成 location /nginxui/ (后面加个 / )就可以正常使用,对了,记得配置 WebSocket 相关。

Example:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
server {
    listen 80;
    listen [::]:80;
    server_name <ip>;
    location /nginx-ui/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://127.0.0.1:9000/;
    }
}

@0xJacky 0xJacky closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2024
@steveepreston
Copy link

@0xJacky Thank you. Worked for me!
Can you please write a sample without map $http_upgrade $connection_upgrade {} block?

@0xJacky
Copy link
Owner

0xJacky commented Sep 9, 2024

@0xJacky Thank you. Worked for me!

Can you please write a sample without map $http_upgrade $connection_upgrade {} block?

Sorry I can't, we need this block to set up a condition where a normal HTTP request will not upgrade to a WebSocket connection.

@steveepreston
Copy link

@0xJacky I tested this and seems everything works:

    location /nginx-ui/ {
        proxy_pass http://127.0.0.1:9000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

The line proxy_set_header Connection "upgrade"; sets the Connection header to "upgrade" for all requests. However, normal HTTP requests typically do not include the Upgrade header, so they will not trigger a WebSocket handshake. This means that the Connection: upgrade header will not affect normal HTTP requests, as they won't initiate a WebSocket connection without the corresponding Upgrade header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants