Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nginx for Port forwarding

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Nginx to do port forwarding, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Sometimes we use some java or node applications, but we don't want them to listen on port 80 directly, so we need port forwarding.

Forward the domain name to the local port

First of all, the most commonly used one is to forward the domain name to another local port.

Server {listen 80; server_name tomcat.shaochenfeng.com; index index.php index.html index.htm; location / {proxy_pass http://127.0.0.1:8080; # forwarding rules proxy_set_header Host $proxy_host; # modify the forwarding request header so that applications on port 8080 can receive real requests proxy_set_header X-Real-IP $remote_addr Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

In this way, access to http://tomcat.shaochenfeng.com will be forwarded to local port 8080.

Forward the domain name to another domain name server {listen 80; server_name baidu.shaochenfeng.com; index index.php index.html index.htm; location / {proxy_pass http://www.baidu.com; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

When accessing http://baidu.shaochenfeng.com, it will be forwarded to http://www.baidu.com.

Forward one local port to another port or another domain name server {listen 80; server_name 127.0.0.1; # public network ip index index.php index.html index.htm; location / {proxy_pass http://127.0.0.1:8080; # or http://www.baidu.com proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

In this way, access to http://127.0.0.1 will be forwarded to local port 8080 or http://www.baidu.com

Plus / with or without /

When configuring proxy_pass proxy forwarding, if url is followed by /, it indicates the absolute root path; if there is no /, it indicates the relative path

For example

Plus /

Server_name shaochenfeng.comlocation / data/ {proxy_pass http://127.0.0.1/;}

Access to http://shaochenfeng.com/data/index.html will be forwarded to http://127.0.0.1/index.html

Do not add /

Server_name shaochenfeng.comlocation / data/ {proxy_pass http://127.0.0.1;}

Access to http://shaochenfeng.com/data/index.html will be forwarded to http://127.0.0.1/data/index.html

The above is how to use Nginx to do port forwarding, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report