近日朋友要我帮他调服务器,
一. 初步需求如下:
1. 一台服务器下要放三个应用,对应三个域名:www.aaa.com,www.bbb.com,www.ccc.com.
2. 其中后两个应用也要可以用bbb.com和ccc.com来访问
我的解决方案:
1. 分别配置三个tomcat实例对应三个应用
2. 配置nginx对应三个域名
本文详细说下第二步
1.在nginx/conf下新建一个文件夹domains
2. 建立三个文件www.aaa.com,www.bbb.com,www.ccc.com
以www.bbb.com为例,打开该文件并键入:
upstream tomcat_www.bbb.com {
#对应的tomcat实例的端口和ip server 127.0.0.1:8030 weight=10 max_fails=2 fail_timeout=30s; } server { listen 80;
#server_name 三个应用各自填写自己的域名 server_name www.bbb.com; index index.html index.htm index.jsp; access_log /usr/nginx/logs/www.bbb.com/www.bbb.com_access.log main; error_log /usr/nginx/logs/www.bbb.com/www.bbb.com_error.log warn; # chunkin on; error_page 411 = @my_error; location @my_error { # chunkin_resume; }
#关键,此处指定应用存放的路径 root /export/data/tomcatRoot/www.bbb.com/; location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#此处和upstream后面的文本保持一致,否则无法实现效果 proxy_pass http://tomcat_www.bbb.com; expires 1d; } location /logs/ { autoindex off; deny all; } }
#以上是配置应用对应各自域名的
#下面是配置不带www的域名加301跳转
server
{
listen 80;
#此处的参数要去掉www
server_name bbb.com;
index index.html index.htm index.jsp;
access_log /usr/nginx/logs/www.bbb.com/www.bbb.com_access.log main;
error_log /usr/nginx/logs/www.bbb.com/www.bbb.com_error.log warn;
# chunkin on;
error_page 411 = @my_error;
location @my_error {
# chunkin_resume;
}
#配置url重写
rewrite ^/(.*) http://www.bbb.com/$1 permanent;
location / {
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcat_www.bbb.com ;
expires 1d;
}
location /logs/ {
autoindex off;
deny all;
}
}
按照此例,分别建立好另外两个应用,保存即可。