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 configure non-80 port forwarding in Nginx server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to configure non-80 port forwarding in the Nginx server". The content is detailed, the steps are clear, and the details are handled properly. I hope this "how to configure non-80 port forwarding in the Nginx server" article can help you solve your doubts.

Nginx can be easily configured as a reverse proxy server:

Server {listen 80; server_name localhost; location / {proxy_pass http://x.x.x.x:9500; proxy_set_header host $host:80; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header via "nginx";}}

But if the listening port of nginx is not the default port 80, change to another port, such as port 81.

Request.getserverport () in the back-end server cannot get the correct port and still returns 80

When response.sendredirect (), the client may not be able to get the correct redirect url.

Let's take a detailed look at the correct configuration method:

Add nginx virtual host

If you want to forward nginx, of course you have to configure nginx. You can enhance the functionality of nginx by adding virtual host configurations. First of all, take a look at the nginx configuration file, the author's nginx file is in / etc/nginx/nginx.conf. From the figure above, you can see that nginx finally introduced the configuration file under the vhosts.d directory. Then create a file with the suffix .conf in the / etc/nginx/vhosts.d directory (if the directory doesn't exist, you need to create it yourself).

Nginx does non-80 port forwarding

To do forwarding, you can use the proxy_pass configuration item of nginx. Nginx listens on port 80 and forwards it to the url to be forwarded after receiving the request. The specific configuration is as follows:

Server {server_name www.test.com listen 80; location / {proxy_pass http://127.0.0.1:8080;}}

Yes, it's that simple. This is the core of configuring port forwarding.

However, when you encounter a business that needs to get a real ip, you also need to add a configuration for the real ip:

Server {server_name www.test.com listen 80; location / {proxy_pass http://127.0.0.1:8080; proxy_set_header host $host:80; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;}}

The configuration of proxy_set_header is the request header for changing http. Host is the hostname of the request, and x-real-ip is the real ip,x-forwarded-for of the request, indicating who initiated the request.

After reading this, the article "how to configure non-80 port forwarding in Nginx server" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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

Internet Technology

Wechat

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

12
Report