This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

This paper introduces how Nginx + Tomcat reverse proxy can efficiently deploy multiple sites on a single server. It has certain reference value, and interested people can learn about it.

Nginx + Tomcat reverse proxy load balancing cluster deployment guide, feel is quite practical, but the general cluster deployment is based on the large volume of traffic, may not be used by some enterprises, similar to the official website of some enterprises, the traffic is not very large, based on this new demand, Nginx + Tomcat reverse proxy can be used to deploy multiple sites on a single server and save on server costs. First of all, you need to install Nginx, JDK, and Tomcat, as described in the previous article. Let’s take a look at our requirements. I have three websites that the project needs to deploy (corresponding to three domain names in turn), provide a Linux server, and access to the corresponding domain name to jump to the corresponding website.

Let’s assume that the Public IP address of the Linux server in the figure is 192.168.2.100, and then assume that you want to set up three sites on this server: www.nginxtest.net, admin.nginxtest.net, and app.nginxtest.net. Ok, down we specific configuration:

To configure the Nginx reverse proxy, we need to connect SSH first and then perform the following operations (generally, it is not recommended to change the default main configuration file nginx.conf, so we create the load balancing configuration file FXDL.

$SSH [email protected] //SSH connection # CD /usr/local/nginx/conf # touch FXDL. Then press the keyboardCopy the code

Note: In the VI editor, press I to enter the INSERT state and press Esc to exit the INSERT state. Then enter the following configuration code (the domain name part is custom changed to your own domain name, and the comments part is enabled as required) :

# Set user nobody to user nobody for security purposes; # Worker_process4; 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; 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 /data/wwwlogs/access_nginx.log main; # Enable efficient file transfer mode sendfile on; Tcp_nopush on; Tcp_nodelay on; Keepalive_timeout 65; # Types_hash_max_size = 2048; # types_hash_max_size = 2048; # file extensions and file type mapping table include/usr/local/nginx/conf/mime types; # default file type default_type application/octet-stream; #include /etc/nginx/conf.d/*.conf; Upstream tomcat_client {server localhost:8080; } #gzip on; #gzip on; Server {listen 80; server_name app.nginxtest.net; location / { proxy_pass http://tomcat_client; proxy_redirect default; Proxy_set_header Host $Host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 80; server_name admin.nginxtest.net; location / { proxy_pass http://tomcat_client; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 80; server_name www.nginxtest.net; location / { proxy_pass http://tomcat_client; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } / location = {# determine whether for the Mobile end if ($http_user_agent ~ * '(iPhone | ipod | the | Android | Windows Phone Mobile | | Nokia)') {rewrite . http://www.nginxtest.net/phone break; } rewrite . http://www.nginxtest.net/pc break; }}}Copy the code

Well, that’s it. Nginx reverse proxy configuration is complete. Configure Tomcat: 2. Configure Tomcat to deploy multiple sites

# $SSH [email protected] / / SSH connection CD/usr/local/tomcat # cp/usr/local/tomcat/conf/server. The XML / usr/local/tomcat/conf/server xml_bk / / backup server XML original file # vi server. The XML / / vi editor to open the file, and then press the keyboard ICopy the code

We edit server.xml and add the following HOST node to the Engine node (the domain name and site project directory in the node need to be customized) :

<Host name="www.nginxtest.net" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="/" docBase="/data/wwwroot/www.nginxtest.net/WebContent" reloadable="true"/> </Host> <Host name="admin.nginxtest.net" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context  path="/" docBase="/data/wwwroot/admin.nginxtest.net/WebContent" reloadable="true"/> </Host> <Host name="app.nginxtest.net" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="/" docBase="/data/wwwroot/app.nginxtest.net/WebContent" reloadable="true"/> </Host>Copy the code

Note: If you want direct access to public IP addresses without web page effect, remove the Engine node

<Host name="localhost" .. >.. </Host>Copy the code

Can. When you have finished typing, press Esc and type:

:wq!
Copy the code

You can save and exit the configuration file. At this point, Tomcat is configured. Next we can place our multi-site project code under /data/wwwroot/. Then start Nginx and Tomcat. Now try to access the secondary domain www.nginxtest.net, admin.nginxtest.net, app.nginxtest.net for each site, and we find that we can jump to the corresponding site (and deploy a server for each site, You see the same effect). That’s all for this article, hope to help you learn, and hope you can support the script family more.