Deploy multiple Web applications on one server through Nginx

Posted by TomatoLover on Sat, 19 Feb 2022 14:55:52 +0100

Deploy multiple Web applications on one server through Nginx

Through the reverse proxy of nginx, you can listen to the two default ports 80(http) and 443(https), and then map the two port requests to the actual running port of the project.

First of all, it is explained here that multiple items of reverse proxy can be distinguished through the request path or different request domain names through one nginx configuration. Whether it can be distinguished according to the request path mainly depends on whether the project supports non root path (for example, baidu.com/a is not a root path, and there is an additional request path a)

Multiple projects are deployed on one server and can be correctly accessed by the external network

0. Install nginx

First, you need to install nginx on the server. nginx-1.18.0 is used here.

For specific installation, please refer to this article: https://www.cnblogs.com/lywJ/p/10710361.html

1. Run all projects on the server

Suppose there are two projects, which have been packaged into jars (built-in tomcat), namely apple jar,banana.jar.

You can start two projects using the following command

nohup java -jar apple.jar &
nohup java -jar banana.jar &

You can write commands to a shell file, such as start SH, just execute sh start The SH command starts the project

#!/bin/bash
nohup java -jar apple.jar &
nohup java -jar banana.jar &
# Nohup indicates that the background is suspended& Indicates that the print information of the project is written to nohup Out file
# In addition, you can add -- server after the command Port = 8080 to change the original project configuration

Or you can know whether each startup is successful through simple judgment

#!/bin/bash
# With apple Jar as an example
echo "application is starting"
nohup java -jar apple.jar &

# wait for 10s, judge if the app is running
sleep 10
# check whrether the project is started successfully
if test $(pgrep -f apple|wc -l) -eq 0
then
        echo "Application Start Failed"
else
        echo "Application Start Successed"
fi
# Find the apple.jar process
ps -ef | grep apple

The command to close the project can also be written as a shell file: stop sh

#!/bin/bash
# With apple Jar as an example
PID=$(ps -ef | grep apple.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi

Now assume that apple Jar occupies port 8080, banana Jar occupies port 8081

2. Configure nginx

Note: the following content involves configuring https, so it is assumed that the domain name is test cn

Suggestion: modify nginx A copy should be made before conf to prevent errors from being modified and can be restored at any time.

cp conf/nginx.conf conf/nginx_copy.conf 

one ️⃣: Distinguish by request path

# HTTPS server
    server {
        listen       443 ssl;
        server_name  localhost;
		# Location of ssl Certificate (nginx version), a pem file and a key file
        ssl_certificate      /usr/local/nginx/ssl/test.cn_bundle.pem;
        ssl_certificate_key  /usr/local/nginx/ssl/test.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        # apple.jar project
        location /a {
            proxy_pass http://localhost:8080/a/;
        }
        # banana.jar project
        location /b {
        	proxy_pass http://localhost:8081/b/;
        }
    }

Note: the root path of the project should be the same as the request path (i.e. location /a and localhost:8080/a are the same). Otherwise, problems may occur. Because if the project does not add a path / A to all APIs and static resources, 404 will appear when requesting static resources, because it is requested through the root path (for example: https://test.cn/xxx.css ,, without adding the request path / a), so the corresponding resource cannot be found.

two ️⃣: Distinguish by domain name

A domain name can have multiple secondary domain names, so you can distinguish different applications with the help of secondary domain names, such as a.test cn,b.test.cn. After purchasing the primary domain name, you only need to add the corresponding secondary domain name in DNS resolution to bind with the server. = = > reference resources

Now suppose there is a domain name test Cn and secondary domain name b.test Cn, both domain names are bound to the server 106.52.230.210.

Note: if https needs to be configured, both the primary domain name and the secondary domain name need to apply for a free ssl certificate. They are different, but there are also universal ssl certificates. The primary domain name and the secondary domain name can be common, but they need to spend money

Configure nginx

##### apple.jar project
server {
        listen       443 ssl;
        # server_name  localhost;
        server_name  test.cn;

        ssl_certificate      /usr/local/nginx/ssl/test.cn_bundle.pem;
        ssl_certificate_key  /usr/local/nginx/ssl/test.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        # mangshangxiadan project
        location / {
            proxy_pass https://localhost:8080/;
        }
    }
 
###### banana.jar project 
server {
        listen       443 ssl;
        server_name  b.test.cn;

        ssl_certificate      /usr/local/nginx/ssl/b.test.cn_bundle.pem;
        ssl_certificate_key  /usr/local/nginx/ssl/b.test.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            proxy_pass http://localhost:8081/;
        }
        # modify the maximum file upload size
        client_max_body_size 2000m; # If there is file upload and download function, it should be modified to an appropriate value, which is 1m by default
  }

The above nginx is configured with https, but http is similar, except that ssl certificate does not need to be configured and the listening port is different. = = => reference resources

other

Configure http to automatically jump to https

# http change to https
server {
    listen 80;
    server_name survey.jxausc.cn;
    #Convert request to https
    rewrite ^(.*)$ https://$host$1 permanent;
}

be careful:

  • The server needs to open the corresponding port after opening the firewall
  • Alibaba cloud, Tencent cloud, etc. have corresponding security group policies. If the corresponding ports cannot be accessed, they need to be configured

📘 reference material

  1. Add secondary domain name to Tencent cloud
  2. Nginx single server deploys multiple websites and domain names
  3. Let your server support http and https and run multiple projects at the same time (jar package and war package deployment)

Topics: Front-end Nginx server Project