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

Nginx configuration reverse proxy

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

Share

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

I. the reverse agency function of the preface

Hide the server information-> ensure the security of the intranet. Usually the reverse proxy is used as the public network access address. The web server is the intranet, that is, the web server is accessed through the external network through nginx configuration.

Give an example

For example, the address of the editor's personal blog is: http://zhengqingya.gitee.io/blog/. Now the editor wants to access the address of the personal blog on the cloud through his own server address http://www.zhengqing520.com/blog/, and the access address is his own server ip or domain name address. In this case, we can configure a reverse proxy through Nginx to achieve this.

Second, how to configure the reverse proxy for Nginx?

We can configure it through proxy_pass.

(1) find the nginx configuration file nginx.conf warm Tip

The editor pulls the nginx through docker, and the default configuration file is the default.conf file included in nginx.conf.

That is to say, the nginx.conf configuration file has the following configuration

Include / etc/nginx/conf.d/*.conf; (2) modify configuration-> implement reverse proxy

Note: here the editor mentions the contents of my default.conf configuration file to the nginx.conf configuration file to implement

That is, comment include / etc/nginx/conf.d/*.conf

Simple configuration

For example, www.zhengqing520.com forwarded to http://zhengqingya.gitee.io

Server {listen 80; server_name www.zhengqing520.com;# server address or bound domain name location / {# all paths after accessing port 80 are forwarded to root / usr/share/nginx/html; index index.html index.htm; proxy_pass http://zhengqingya.gitee.io; in ip configured by proxy_pass # configure ip address and port number of reverse proxy [Note: url address needs to be added with http:// or https://]}} complex configuration

Access different server addresses according to different suffix names

Www.zhengqing520.com/api forwarded to http://www.zhengqing520.com:9528/api/www.zhengqing520.com/blog/ forwarded to http://zhengqingya.gitee.io/blog/server {listen 80; server_name www.zhengqing520.com;# server address or bound domain name location ^ / api {# ^ ~ / api to match the request with api prefix proxy_pass http://www.zhengqing520.com:9528/api/; # Note: proxy_pass ends with /,-> effect: the path after / api/* will be directly spliced into the latter # proxy_set_header function: set the request header value sent to the backend server (above proxy_pass) # [when Host is set to $http_host, the request header value will not be changed # when Host is set to $proxy_host, the Host information in the request header is reset; # when it is a $host variable, its value is the value of the Host field when the request contains the Host request header, and the primary domain name of the virtual host when the request does not carry the Host request header # when it is $host:$proxy_port, send ex with the port: $host:8080] proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # to get the real ip of the user on the web server, you need to configure ① [$remote_ addr value = user ip] proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for # to obtain the real ip of a user on the web server, you need to configure the condition ② proxy_set_header REMOTE-HOST $remote_addr; # proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for variable = X-Forwarded-For variable} location ^ ~ / blog/ {# ^ ~ / blog/ means to match the request proxy_pass http://zhengqingya.gitee.io/blog/; with the prefix blog/ Proxy_set_header Host $proxy_host; # change request header-> forward to Cloud proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true;}} III. Summary

Here are all the contents of the editor's nginx configuration file for reference.

User nginx;worker_processes 1 errorists log / var/log/nginx/error.log warn;pid / var/run/nginx.pid;events {worker_connections 1024;} http {include / etc/nginx/mime.types; default_type application/octet-stream Log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log / var/log/nginx/access.log main; sendfile on; # tcp_nopush on; keepalive_timeout 65; # gzip on # include / etc/nginx/conf.d/*.conf; # introduces the default.conf configuration file server {listen 80; server_name www.zhengqing520.com;# server address or bound domain name # charset koi8-r; # access_log / var/log/nginx/host.access.log main # start-location / {root / usr/share/nginx/html Try_files $uri $uri/ @ router; index index.html index.htm; # proxy_pass http://zhengqingya.gitee.io; # proxy ip address and port number # proxy_connect_timeout 600; # proxy connection timeout (in milliseconds) # proxy_read_timeout 600 # read resource timeout of the agent (in milliseconds)} location @ router {rewrite ^. * $/ index.html last;} location ^ ~ / api {# ^ ~ / api/ means to match the request proxy_pass http://www.zhengqing520.com:9528/api/; with the prefix api # Note: proxy_pass ends with /,-> effect: the path after / api/* will be directly spliced into the latter # proxy_set_header function: set the request header value sent to the backend server (above proxy_pass) # [when Host is set to $http_host, the request header value will not be changed # when Host is set to $proxy_host, the Host information in the request header is reset; # when it is a $host variable, its value is the value of the Host field when the request contains the Host request header, and the primary domain name of the virtual host when the request does not carry the Host request header # when it is $host:$proxy_port, send ex with the port: $host:8080] proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # to get the real ip of the user on the web server, you need to configure ① [$remote_ addr value = user ip] proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for # to obtain a user's real ip on the web server, you need to configure the condition ② proxy_set_header REMOTE-HOST $remote_addr; # proxy_set_header X-Forwarded-For $http_x_forwarded_for # $http_x_forwarded_for variable = X-Forwarded-For variable} location ^ ~ / blog/ {# ^ ~ / blog/ means to match the request proxy_pass http://zhengqingya.gitee.io/blog/; proxy_set_header Host $proxy_host with the prefix blog/ # change request header value-> forward to Cloud proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true } # end-# error_page 404 / 404.html # redirect server error pages to the static page / 50x.html # error_page 500 502 503 504 / 50x.html; location = / 50x.html {root / usr/share/nginx/html;}

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