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

How to proxy nginx request to tomcat path?

· 52CE 我爱测

I have a Domain Name example.com. When enter example.com I want to proxy the request to 127.0.0.1:8080/source My nginx configuration location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pa…

问题原文

原帖链接 · Mattia Dinosaur · CC BY-SA 3.0

I have a Domain Name example.com. When enter example.com I want to proxy the request to 127.0.0.1:8080/source

My nginx configuration

location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://127.0.0.1:8080;
 }

This works for 127.0.0.1:8080, but I can't get it to proxy to the /source subidrectory. When I change to

proxy_pass http://127.0.0.1:8080/source/;

It doesn't work correctly.

It redirect to http://127.0.0.1:8080/source/ good, but the js and css file path is /source/js/ *.js and /source/css/ *.css , and other resource file is /source/*. For example , when brower load the html , it will try to find the /source/js/ *.js, for the reason that I redirect to http://127.0.0.1:8080/source/ , the finnally path is http://127.0.0.1:8080/source/source/js/ *.js , but the right path is http://127.0.0.1:8080/source/js/ *.js

I just want that when i enter the example.com , it will redirect to http://127.0.0.1:8080/source/ , or redirect to example.com/source is ok too.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The end of your question suggests that all you need to do is rewrite a single URI. This can be accomplished a number of ways, but an exact match location with a rewrite...last would look like this:

location = / {
    rewrite ^ /source/ last;
}
location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://127.0.0.1:8080;
}

See this and this for details.

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

资料来源与适用范围

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

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

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