Nginx多站点设置及负载均衡

apache端口88 tomcat端口8080

多个.conf方法(优点是灵活,缺点就是站点比较多配置起来麻烦)

这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,假设:

IP地址: 1.1.1.1
域名1 example1.com 放在 /webapp/example1
域名2 example2.com 放在 /webapp/example2

 

配置 nginx virtual hosting 的基本思路和步骤如下:

 把2个站点 example1.com, example2.com 放到 nginx 可以访问的目录 /webapp/
给每个站点分别创建一个 nginx 配置文件 example1.com.conf,example2.com.conf, 并把配置文件放到 /usr/local/nginx/vhosts/
然后在 nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)
重启 nginx

 

具体过程
下面是具体的配置过程:

1、在 /usr/local/nginx 下创建 vhosts 目录  mkdir /usr/nginx/vhosts

2、在 /usr/local/nginx/vhosts/ 里创建一个名字为 example1.com.conf 的文件,把以下内容拷进去
server {
        listen  80;
        server_name example1.com www.example1.com; access_log  /webapp/example1/logs/access_ example1.log;

        if (-d $request_filename){
 	   rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
 	}
 	
 	# 动态页面,交给tomcat处理
 	location ~ \.(jsp|jspx|do|action)?$ {
 	   proxy_set_header Host $host;
 	   proxy_set_header X-Forwarded-For $remote_addr;
 	   proxy_pass http://tomcat_proxy;
 	}

	# 动态页面,交给apache处理
 	location ~ \.(php)?$ {
 	   proxy_set_header Host $host;
 	   proxy_set_header X-Forwarded-For $remote_addr;
 	   proxy_pass http://apache_proxy;
 	}

	location /training/ { 
            proxy_pass        http://tomcat_proxy;
            proxy_set_header  Host             $host; 
            proxy_set_header  X-Real-IP        $remote_addr; 
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; 
            #sub_filter        /training/          /; 
        } 

        # 用户浏览器端的缓存设置
 	    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
 	      expires 7d;
        }

	location ~ .*\.(js|css)?$ {
 	      expires 24h;
 	}


        location / {
            root   /webapp/example1/www;
            index index.html index.htm index.php index.jsp;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/nginx/html;
        }

        location ~ /.ht {
            deny  all;
        }
}


3、打开 /usr/local/nginx/conf/nginix.conf 文件,在相应位置加入 include 把以上文件包含进来

# main server config (http part)
http {
	include       mime.types;
	default_type  application/octet-stream;

         #关闭http header 中关于服务器的版本号
           #server_tokens  off;

	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  logs/access.log  main;

	sendfile on;
	tcp_nopush on;
         tcp_nodelay on;

    	#keepalive_timeout  0;
	keepalive_timeout  65;

         server_names_hash_bucket_size 128;
 	client_header_buffer_size 32k;
 	large_client_header_buffers 4 32k;
 	client_max_body_size 3m;
 	client_body_buffer_size 512k;

	# 代理的相关参数设置
 	 proxy_connect_timeout 5;
 	 proxy_read_timeout 60;
 	 proxy_send_timeout 5;
 	 proxy_buffer_size 16k;
 	 proxy_buffers 4 64k;
 	 proxy_busy_buffers_size 128k;
 	 proxy_temp_file_write_size 128k;
 	
 	# 启用gzip压缩,提高用户访问速度
 	 gzip on;
 	 gzip_min_length 1k;
 	 gzip_buffers 4 16k;
 	 gzip_http_version 1.1;
 	 gzip_comp_level 2;
 	 gzip_types text/plain application/x-javascript text/css application/xml;
 	 gzip_vary on;

          # 配置需要代理的tomcat
 	upstream tomcat_proxy{
	   ip_hash;
           session_sticky;
 	   server localhost:8080 max_fails=3 weight=1 fail_timeout=60s;
 	}

	# 配置需要代理的apache
 	upstream apache_proxy{
	   ip_hash;
           session_sticky;
 	   server localhost:88 max_fails=3 weight=1 fail_timeout=60s;
 	}

	server {
        	listen         80;
        	server_name     _;
        	access_log      /var/local/nginx/logs/access.log;
         index index.html index.htm index.jsp index.php;

        	if (-d $request_filename){
 	   rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
 	}
 	
 	# 动态页面,交给tomcat处理
 	location ~ \.(jsp|jspx|do|action)?$ {
 	   proxy_set_header Host $host;
 	   proxy_set_header X-Forwarded-For $remote_addr;
 	   proxy_pass http://tomcat_proxy;
 	}

	# 动态页面,交给apache处理
 	location ~ \.(php)?$ {
 	   proxy_set_header Host $host;
 	   proxy_set_header X-Forwarded-For $remote_addr;
 	   proxy_pass http://apache_proxy;
 	}


        #charset koi8-r;

        #access_log  logs/host.access.log  main;


	location /training/ { 
            proxy_pass        http://tomcat_proxy;
            proxy_set_header  Host             $host; 
            proxy_set_header  X-Real-IP        $remote_addr; 
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; 
            #sub_filter        /training/          /; 
        } 
        # 用户浏览器端的缓存设置
 	    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
 	      expires 7d;
        }

	location ~ .*\.(js|css)?$ {
 	      expires 24h;
 	}
 	    
 	     access_log off;
 	     #charset koi8-r;
 	     #access_log logs/host.access.log main;

	}

         #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;
        }

        # 包含所有的虚拟主机的配置文件
         include /usr/local/nginx/vhosts/*;
}

4、apache多站点设置 

1.让Apache在启动时能加载虚拟主机模块。
打开Apache安装目录下conf/httpd.conf文件,找到下面一行文字,把最前面的 # 号去掉,然后保存。
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
2.接着找到同一文件中的DocumentRoot和Directory,改为站点目录的上一级目录
例如站点放在 /webapp/example1/www,则改为以下形式
DocumentRoot"/webapp"
<Directory"/webapp"> 
3.配置完成后文件在最后添加如下:
DocumentRoot是文件放置路径,ServerName是网站域名:
<VIRTUALHOST *:88>
    ServerAdmin webmaster@example.com
    DocumentRoot "/webapp/example1/www "     #web目录路径
   ServerName example.com           #host名称
   ServerAlias www.example.com
    ErrorLog "/webapp/example1/logs/dummy-host.example.com-error.log"
    CustomLog "/webapp/example1/logs/dummy-host.example.com-access.log" common
</virtualhost>

5、tomcat多站点设置

1 打开tomcat/conf/server.xml,在里面找到<Engine name="Catalina" defaultHost="localhost">.....</Engine>
2 在<Engine name="Catalina" defaultHost="localhost"></Engine>中间加入内容:
<Engine name="Catalina" defaultHost="localhost">
.........原有内容不要动
   下面为新加内容:

 <Host name="example.com" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
 <Context path="/"  crossContext="true" reloadable="true" docBase="/webapp/example1/www" /> 
</Host>
其中/webapp/example1/www是web应用程序目录

 

6、重启服务

重启 Nginx
/etc/init.d/nginx restart
重启apache
/etc/init.d/httpd restart
重启tomcat
cd /ilkhome/apache-tomcat-8.0.36/bin/
./shutdown.sh
./startup.sh


 

上一篇:加速查询MaxCompute再对接Quick BI,选交互式分析就对了!


下一篇:RavenDB起步--文档标识符