nginx+tomcat单个域名及多个域名配置

简介:

同步首发:http://www.yuanrengu.com/index.php/20171130.html

项目开发接近尾声,开始着手在生产环境部署项目,开发阶段部署项目都没用nginx。项目是采用SOA架构,多系统开发,主要包括服务系统、中台系统、后台系统、金融系统、接口系统、调度系统、报表系统等。这类分布式的系统,一般也都会用到nginx来做负载均衡。

从公司刚成立就进来,赶鸭子上架来做架构师,负责公司的所有研发事情,搭建公司的整个技术架构,起初的所有核心业务代码基本都由自己亲自把关来进行编码。系统也从最初的只有一个pc端,发展到如今pc中台、后台、android端3个app、iOS端3个app,产品越做越多,亲自负责招聘面试、培训。之前很多时候都有过无助和苦恼,因为负责公司整个架构,又要负责核心业务的编码,技术难点的攻克,新员工的招聘及培训,现在团队已经都发展到16个人,而且这全是研发人员。

回想这一路,觉得之前看似爬不过去的山也不过如此,也许这就是成长吧,成长总是会伴随些许汗水与泪水吧。由于是负责团队的所有事情,所以数据库的维护、迁移数据、建索引等性能优化,项目部署等所有事情必须得一肩挑,不要问我为什么公司没有DBA?为什么没有运维?我真的只能给你一个眼神,让你慢慢去体会。

话不多说,直接开始技术干货分享。

nginx做负载均衡的优势网上有很多介绍资料,这里我不再多做介绍。因为有很多系统要部署,涉及到域名、二级域名、多个域名等的部署。在实际的部署由于对nginx的不够熟悉,遇到过很多坑,其中这种多域名的配置,xxxx.com转发到www.xxxx.com、访问域名转发到tomcat里的项目等,现在先总结一部坑的解决办法。

如将xxxx.com这个域名指向8082端口里的tomcat项目,在做这个介绍前先讲个插曲,如访问xxxx.com需转向到www.xxxx.com,这一点很多人都会忽略。

现在如果要部署中台、后台、金融系统,找到nginx/conf/nginx.conf,修改配置:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
upstream web{
     server localhost:8082;
}
 
upstream admin{
     server localhost:8083;
}
 
upstream finance{
     server localhost:8084;
}
 
 
server {
     listen       80;
     server_name  finance.xxxx.com;
 
     #charset koi8-r;
 
     #access_log  logs/host.access.log  main;
 
     location / {
         proxy_pass http: //finance ;
         proxy_set_header Host $http_host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
 
     #error_page  404              /404.html;
 
     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504   /50x .html;
     location =  /50x .html {
         root   html;
     }
}
 
 
server {
     listen       80;
     server_name  www.xxx.com;
 
     #charset koi8-r;
 
     #access_log  logs/host.access.log  main;
 
     location / {
         proxy_pass http: //web ;
         proxy_set_header Host $http_host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           
     }
 
     #error_page  404              /404.html;
 
     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504   /50x .html;
     location =  /50x .html {
         root   html;
     }
 
     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     #
     #location ~ \.php$ {
     #    proxy_pass   http://127.0.0.1;
     #}
 
     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     #
     #location ~ \.php$ {
     #    root           html;
     #    fastcgi_pass   127.0.0.1:9000;
     #    fastcgi_index  index.php;
     #    fastcgi_param  SCRIPT_FILENAME  /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;
     #}
}
              
server {
     server_name xxxx.com;
     rewrite ^(.*) http: //www .xxxx.com$1 permanent;
}
 
 
server {
     listen       80;
     server_name  admin.xxxx.com;
 
     #charset koi8-r;
 
     #access_log  logs/host.access.log  main;
 
     location / {
         proxy_pass  http: //admin ;
         proxy_set_header Host $http_host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           
     }
 
     #error_page  404              /404.html;
 
     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504   /50x .html;
     location =  /50x .html {
         root   html;
     }
 
     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     #
     #location ~ \.php$ {
     #    proxy_pass   http://127.0.0.1;
     #}
 
     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     #
     #location ~ \.php$ {
     #    root           html;
     #    fastcgi_pass   127.0.0.1:9000;
     #    fastcgi_index  index.php;
     #    fastcgi_param  SCRIPT_FILENAME  /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;
     #}
}

 上面的配置还包括了访问xxxx.com转向www.xxxx.com的配置,如下:

1
2
3
4
server {
      server_name xxxx.com;
      rewrite ^(.*) http: //www .xxxx.com$1 permanent;
  }

nginx的基本配置大致就是这样,如果绑定多个域名(不管是一级域名还是二级域名),需配置多个server,你会发现这几个server配置都差不多,主要是更改server_name及proxy_pass指向即可。upstream节点其实就是代理服务的访问路径。

如果此时访问域名,你会发现nginx的配置生效了,只是目前显示的是tomcat的默认界面。nginx的配置基本就这样了,接下来对tomcat做些配置的修改。找到tomcat里的conf/server.xml,注释掉默认的Host配置,添加如下Host配置:

1
2
3
4
5
6
<Host name= "localhost"  appBase= "E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web"  deployOnStartup = "false"  autoDeploy= "false"  unpackWARs= "true" >
              <Context path= "/"   docBase= "E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web"  />
              <Valve   className= "org.apache.catalina.valves.AccessLogValve"  
              directory= "logs"      prefix= "localhost_access_log"    suffix= ".txt"  
              pattern= "%h %l %u %t " %r " %s %b"    />
< /Host >

以上是windows服务器下的配置,如为linux,只需更改appBase和docBase,指向项目的路径。tomcat的配置也已经完成,重启tomcat,访问域名就指向了tomcat里的项目。


本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/7941099.html,如需转载请自行联系原作者

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
11月前
|
应用服务中间件 nginx
Nginx基于域名\端口的虚拟主机
Nginx基于域名\端口的虚拟主机
|
域名解析 应用服务中间件 nginx
Nginx/阿里云/二级域名的配置和使用
Nginx/阿里云/二级域名的配置和使用
581 0
Nginx/阿里云/二级域名的配置和使用
|
域名解析 网络协议 Java
tomcat的域名以及多域名配置
tomcat的域名以及多域名配置
|
搜索推荐 架构师 应用服务中间件
Nginx极简入门(四)基于域名的虚拟主机配置
前面讲了如何安装配置Nginx,今天要说的是Nginx如何基于域名配置虚拟主机。 需要说明的是:由于本文章是nginx系列文章中的一篇,文章里面很多其他的配置,可能前面的文章已经说讲过,然后后续就没有在介绍,如果出现有些配置没有讲,大家可能需要去看看前面的文章。
Nginx极简入门(四)基于域名的虚拟主机配置
|
编解码 网络协议 应用服务中间件
|
Web App开发 应用服务中间件 PHP
nginx+tomcat单个域名及多个域名配置
同步首发:http://www.yuanrengu.com/index.php/20171130.html 项目开发接近尾声,开始着手在生产环境部署项目,开发阶段部署项目都没用nginx。项目是采用SOA架构,多系统开发,主要包括服务系统、中台系统、后台系统、金融系统、接口系统、调度系统、报表系统等。
1623 0
|
应用服务中间件 容器
tomcat发布应用并配置域名
应用场景 一个web应用,或者网页,网站,在tomcat容器中发布了,想放到公网上,让大家都能访问,而不是仅仅在局域网中,这样如何设置呢? 操作步骤 首先,您得有一个公网服务器,也就是外网IP地址,或者更...
1142 0
|
应用服务中间件 nginx
|
应用服务中间件 nginx 搜索推荐
|
应用服务中间件 PHP Apache