nginx + tomcat 实现二级域名

 

小网站成本有限,用二级域名可以大大降低经济和时间人力的成本,二级域名类似于baidu.com 和tieba.baidu.com的关系。

那么java网站目前我知道的实现方法有2种,tomcat直接实现和nginx + tomcat实现

之前用的tomcat方案效果有点小问题,今天重新用nginx + 多个tomcat再实现一次

 

安装配置nginx

#安装
sudo yum install epel-release
sudo yum install nginx

#启动 start 也sudo systemctl restart nginx       sudo systemctl stop nginx
sudo systemctl start nginx

#配置 每次配置完需要重启服务
vim /etc/nginx/nginx.conf


原版安装完成以后,配置文件里有这样一段,我是直接注释掉的

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 

然后替换为以下的内容

相当于

www.tczmh.club 会访问第一个tomcat的地址

bz.tczmh.club 会访问第二个tomcat的地址

    server {
        listen       80;
        server_name  www.tczmh.club;
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  bz.tczmh.club;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://localhost:8081;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
    }


 

 

 

方案2 是单tomcat下多个项目 也可以与方案1混用

    server {
        listen       80;
        server_name  www.tczmh.club;
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  bz.tczmh.club;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://localhost:8080/bz/;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
    }

 

也可以是多域名对应单项目

    server {
        listen       80;
        server_name  www.tczmh.club;
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  bz.tczmh.club;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://localhost:8080/bz/;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
    }
    
    server {
        listen       80;
        server_name  www.jjbz.xyz;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://localhost:8080/bz/;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
    } 

 

每次改完配置文件都要执行重启服务命令

sudo systemctl restart nginx


 

 

最终实现了单服务器 二级域名的解决方案,

可以访问www.tczmh.club bz.tczmh.club 实现不同项目的效果

还可以继续深入,使用新域名,CNAME方案解析到二级域名。

达到单服务器单项目多域名的效果,节省多服务器的开支。

 

 

 

 

 2018-08-22 更新

增加nginx错误页面以及单位时间内限制请求次数 具体代码如下

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

   limit_req_zone $binary_remote_addr zone=one:32m rate=15r/s; 

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

   # server {
   #     listen       80 default_server;
   #     listen       [::]:80 default_server;
   #     server_name  _;
   #     root         /usr/share/nginx/html;
   #
   #     # Load configuration files for the default server block.
   #     include /etc/nginx/default.d/*.conf;
   #
   #     location / {
   #     }
   #
   #     error_page 404 /404.html;
   #         location = /40x.html {
   #     }
   #
   #     error_page 500 502 503 504 /50x.html;
   #         location = /50x.html {
   #     }
   # }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

    server {
        listen       80;
        server_name  www.tczmh.club;
        location / {
            proxy_pass http://localhost:8080;
            limit_req zone=one burst=5;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;       
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;        
            access_log off;
            break;
        }
       
        error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://cdn.tczmh.club/cat.html;
    }
    server {
        listen       80;
        server_name  bz.tczmh.club;
        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://localhost:8080/bz/;
            limit_req zone=one burst=5;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_connect_timeout 3;
            proxy_read_timeout 3;
            proxy_send_timeout 3;
            access_log off;
            break;
        }
        
        error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://cdn.tczmh.club/cat.html;
   
    }
    #server {
    #    listen       80;
    #    server_name  csyd.tczmh.club;
    #    location / {
    #        #root   html;
    #        #index  index.html index.htm;
    #        proxy_pass http://localhost:8765;
    #        proxy_set_header Host $host:$server_port;
    #        proxy_set_header X-Real-IP $remote_addr;
    #        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #        proxy_set_header X-Forwarded-Scheme $scheme;
    #        proxy_connect_timeout 3;
    #        proxy_read_timeout 3;
    #        proxy_send_timeout 3;
    #        access_log off;
    #        break;
    #    }
    #}
}

 

 

 

 

送人玫瑰,手留余香
tomcat开启gzip
利用MOB API接口,纯前端实现用登陆、注册、个人信息增删改查等功能