Detailed Nginx + Tomcat reverse proxy How to deploy multiple sites efficiently on a single server _nginx

Source: Internet
Author: User
Tags ssh tomcat nginx reverse proxy

The previous article shared the Nginx + TOMCAT reverse Agent Load Balancing cluster Deployment Guide, the feeling is quite practical, but the general cluster deployment is based on a large number of visits, some enterprises may not use, similar to some of the corporate website, the visit is not very large, based on this new demand, today to share a special Nginx + Tomcat Reverse proxy How to deploy multiple sites on a single server, save on server expenses, just in this article.

First we need to install a good nginx, JDK, Tomcat, installation method has been said in the previous article, this article will not repeat.

Come down and look at our needs, I have three Web site project engineering needs to be deployed (corresponding to three domain names in turn), to provide a Linux server, to achieve access to the corresponding domain name jump to the corresponding site.

Let's take a look. To solve this problem, I designed the architecture diagram:

Here we assume that the Linux server public network IP is 192.168.2.100, and then assume that you want to build so three sites on this server: Www.nginxtest.NET, Admin.nginxtest.Net, app.nginxtest.net.

All right, come down, we have to configure:

1. Configure Nginx Reverse Proxy

We still connect SSH first, then do the following (generally do not recommend to modify the default main profile nginx.conf, so we create a new load balancing profile fxdl.conf, to ensure that the server security, such as the following):

$ ssh root@192.168.2.100             //ssh Connection
# cd/usr/local/nginx/conf
# Touch fxdl.conf                 //Create Agent Profile
# VI fxdl.conf                  //VI editor to open the file, and then press the keyboard I

Note: In the VI editor, the keyboard presses I into the insert state, pressing ESC to exit the insert state.

Then enter the following configuration code (the Domain name section is customized to your own domain name, and the annotation section opens on demand):

#设置低权限用户, user Nobody set up for security purposes;

#工作衍生进程数 worker_processes 4;
#设置错误文件存放路径 #error_log Logs/error.log;
#error_log Logs/error.log Notice;

#error_log Logs/error.log Info;

#设置pid存放路径 (PID is an important document in the control system) #pid Logs/nginx.pid;

#设置最大连接数 events{worker_connections 1024;} HTTP {#用来设置日志格式 Log_format main ' $remote _addr-$remote _user [$time _local] "$request" $status $body _byt
  Es_sent "$http _referer" "$http _user_agent" "$http _x_forwarded_for";

  #用来指定日志文件的存放路径 Access_log/data/wwwlogs/access_nginx.log Main;
  #开启高效文件传输模式 sendfile on;
  #防止网络阻塞 Tcp_nopush on;
  #防止网络阻塞 Tcp_nodelay on;
  #长连接超时时间, the unit is seconds Keepalive_timeout 65;

  #散列表的冲突率, the default 1024, the greater the memory consumption, but the hash key conflict rate will be reduced, the retrieval speed is faster types_hash_max_size 2048;
  #文件扩展名与文件类型映射表 include/usr/local/nginx/conf/mime.types;

  #默认文件类型 Default_type Application/octet-stream;

  #include/etc/nginx/conf.d/*.conf; #主要是用于设置一组可以在proxy_pass和fastcgi_pass指令中使用额代理服务器, the default load Balancing mode is polling upstream tomcat_client {Server localhost:8080;

  #开启gzip压缩, when opened, the access page will automatically compress #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 =/{#判断是否为手机移动端 if ($http _user_agent ~* ' (iphone|ipod|ipad| android| Windows phone| mobile|
  Nokia) {rewrite. Http://www.nginxtest.net/phone break;    } rewrite.
    HTTP://WWW.NGINXTEST.NET/PC break;

 }
  }
}

Well, that's it, nginx. Reverse proxy configuration complete. Down to configure Tomcat:

2. Configure Tomcat to deploy multiple sites

$ ssh root@192.168.2.100             //ssh Connection
# cd/usr/local/tomcat
# cp/usr/local/tomcat/conf/server.xml/usr/local /TOMCAT/CONF/SERVER.XML_BK//Backup Server.xml original file
# VI server.xml                  //Open file with VI Editor, then press keyboard I

We edit the Server.xml,engine node under Add the following host node (the domain name and site Engineering directory in the node need to be customized to modify for your own):

 
 

Note: If you want to achieve direct access to public network IP no web effect, delete the engine node under the

 
 

Can.

Press ESC when the input is complete, and then enter:

: wq!

You can save and exit the configuration file. To this end, Tomcat is also configured.

Next we can put our multiple site engineering code under the/data/wwwroot/. Then start Nginx and Tomcat.

Now you can try, respectively, access to each site's level two domain name www.nginxtest.net, admin.nginxtest.net, App.nginxtest.net, we found that we can jump to the corresponding site (and each site to deploy a separate server, see the effect is the same).

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.