前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx多站点设置及负载均衡

Nginx多站点设置及负载均衡

作者头像
似水的流年
发布2019-12-11 21:00:37
1.3K0
发布2019-12-11 21:00:37
举报
文章被收录于专栏:电光石火电光石火

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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-08-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档