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 make a multi-port website use a Nginx for reverse proxy

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

Share

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

This article introduces the knowledge of "how to make a multi-port website use a Nginx for reverse proxy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One: use nginx as a reverse proxy

In order to solve these two problems, the natural first thought is to use reverse proxy, my ideal idea should be like the following picture.

Since all user requests go through nginx, let nginx determine which backend agent the current url needs to jump to, a better strategy should be to let nginx determine what the current host is to decide which webserver to jump to. For example, if a.mip.com jumps to apollo,j.mip.com, it will jump to jenkins. And so on, so it can be solved perfectly, right? In nginx, you can use the if instruction under the rewrite module to judge.

Two: use the if instruction

I would like to mention here that nginx is more primitive, and if you need to use a third-party module, you also need to recompile nginx, which is very troublesome to use, so here simply use OpenResty, which extends nginx, and integrates many mature lua modules to download the latest 1.15.8 by yourself, in exactly the same way as nginx.

The default is to install to the / usr/local/ directory, when you see an openresty directory indicating that you installed successfully.

[root@localhost local] # ls bin etc games include lib lib64 libexec openresty sbin share src [root@localhost local] # pwd / usr/local

Next you can use nginx-v to see the openresty version number or something.

[root@localhost sbin] # pwd / usr/local/openresty/nginx/sbin [root@localhost sbin] # [root@localhost sbin] #. / nginx-v nginx version: openresty/1.15.8.1

For convenience, I just use nginx to open three server:

The first website launched on 192.168.23.129 nginx was proxy.

The second website opened on the 192.168.23.129 8001 nginx, simulating apollo.

The third website opened on 192.168.23.129 8002 nginx, which simulates jenkins.

1. Simulation of apollo

Server {listen 8001; server_name somename alias another.alias; location / {root html; index apollo.html;}}

The default page of the 8001 port website is apollo.html, and the path to this apollo.html is the html directory under nginx, as shown below.

[root@localhost html] # pwd / usr/local/openresty/nginx/html [root@localhost html] # ls 50x.html apollo.html index.html jenkins.html

2. Simulation of jenkins

Server {listen 8002; server_name somename alias another.alias; location / {root html; index jenkins.html;}}

The path of the jenkins.html file is shown above. I won't repeat it.

3. Simulation of proxy

Server {listen 80; server_name localhost; location / {if ($host = "a.mip.com") {proxy_pass http://localhost:8001;} if ($host = "j.mip.com") {proxy_pass http://localhost:8002;}}

As you can see, you only need to use the if conditional statement under the rewrite module to determine the value of the host in the current url by using the $host system variable to jump to the corresponding website.

4. Host mapping

All right, then you just need to map a.mip.com and j.mip.com to the ip address 192.168.23.129 of nginx. Because these domain names are easy to remember rather than real.

192.168.23.129 a.mip.com 192.168.23.129 j.mip.com

5. Start nginx

[root@localhost sbin] #. / nginx [root@localhost sbin] # [root@localhost sbin] # [root@localhost sbin] # netstat-tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 00 0.0.0.0pur8001 0.0.0.0root@localhost sbin * LISTEN 3802/nginx: master tcp 00 0.0.0.0 master tcp 8002 0.0.0.0 LISTEN 3802/nginx: LISTEN 3802/nginx: master tcp 00 0.0.0.0 master tcp 22 0.0.0. 0LISTEN 1172/sshd tcp6 * LISTEN 1172/sshd tcp 0 0 127.0.0.1 LISTEN 1172/sshd tcp6 25 0.0.0.0 * LISTEN 1724/master tcp6 0 0: 22: * LISTEN 1172/sshd tcp6 0 0:: :: * LISTEN 1724/master

As you can see from the picture above, the port 80Query 8001 # 8002 has been opened. Next, you can go to the browser to verify it.

Here are all the nginx.conf.

# user nobody; worker_processes 1; # error_log logs/error.log; # error_log logs/error.log notice; # error_log logs/error.log info; # pid logs/nginx.pid; events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream Log_format main'$host-> $remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log logs/access.log main; sendfile on; # tcp_nopush on # keepalive_timeout 0; keepalive_timeout 65; # gzip on; server {listen 80; server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; # location = / get {# set_unescape_uri $key $arg_key; # this requires ngx_set_misc # redis2_query get $key # redis2_pass 10.105.13.174redis2_pass 6379; #} location / {if ($host = "a.mip.com") {proxy_pass http://localhost:8001;} if ($host = "j.mip.com") {proxy_pass http://localhost:8002; } root html; index index.html index.htm;} # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502503504 / 50x.html; location = / 50x.html {root html } # proxy the PHP scripts to Apache listening on 127.0.0.1 location 80 # # location ~\ .php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 fastcgi_pass 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 root html; 9000; # fastcgi_index index.php; # fastcgi_pass / scripts$fastcgi_script_name # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # # location ~ /\ .ht {# deny all; #} # another virtual host using mix of IP-, name-, and port-based configuration # server {listen 8001 Server_name somename alias another.alias; location / {root html; index apollo.html;}} server {listen 8002; server_name somename alias another.alias; location / {root html; index jenkins.html } # HTTPS server # # server {# listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers high # ssl_prefer_server_ciphers on; # location / {# root html; # index index.html index.htm; #} #}} "how to make a multi-port website reverse proxy with a Nginx" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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