nginx动静分离本地搭建

p2p项目虚拟主机1:192.168.147.200:8080/p2p/

p2p项目虚拟主机2:192.168.147.200:8082/p2p/

dataservice: 192.168.147.200:8081

 

启动顺序:

1.先启动dubbo和redis服务器

2.启动dataservice:8081dubbo-provider

3.在启动8080, 8082的tomcat

4.启动nginx代理

1.首先nginx添加配置轮询策略:nginx.conf

upstream www.p2p.com {

        server  192.168.147.200:8082 weight=2;

        server  192.168.147.200:8080 weight=1;

}

location /p2p {

            proxy_pass http://www.p2p.com;

        }

 

测试1:

浏览器分别启动8080,8082能够访问

nginx轮询访问tomcat成功:

 

 

2.在conf/extra下添加配置虚拟主机配置文件nginx81.conf, nginx82.conf:

cd nginx/conf

mkdir extra

vim nginx81.conf

server {

        listen       81;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

       

        location ~ .*/(css|js|img|images|image) {

            root   /opt/static;

        }

}

--配置文件二:

server {

        listen       82;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

       

        location ~ .*/(css|js|img|images|image) {

            root   /opt/static;

        }

    }

3.主配置文件设置静态拦截

upstream www.p2p.com.static {

        server 127.0.0.1:81;

        server 127.0.0.1:82;

    }

 

location ~ .*/(css|js|img|images|image) {

            proxy_pass http://www.p2p.com.static;

        }

 

末尾添加: include extra/*;

 

4.在nginx服务器opt/static下添加静态文件夹img,js,css,images

cd /opt/static

mkdir p2p

将p2p的静态文件img,js,css,images移到该目录下

 

nginx.conf总配置文件:

#user  nobody;

worker_processes  1;

 

#pid        logs/nginx.pid;

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

    upstream www.p2p.com {

    server  192.168.147.200:8082 weight=2;

        server  192.168.147.200:8080 weight=1;

    }

   

    upstream www.p2p.com.static {

    server 127.0.0.1:81;

    server 127.0.0.1:82;

    }

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

 

    location /p2p {

            proxy_pass http://www.p2p.com;

        }

   

    location ~ .*/(css|js|img|images|image) {

            proxy_pass http://www.p2p.com.static;

        }

        #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 extra/*;

}

nginx81.conf

server {

        listen       81;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

       

        location ~ .*/(css|js|img|images|image) {

            root   /opt/static;

        }

}

nginx82.conf

server {

        listen       82;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

       

        location ~ .*/(css|js|img|images|image) {

            root   /opt/static;

        }

}

 

 

最后依次访问: 192.168.147.200:8080/p2p/ 不能出现css

访问http://192.168.160.150/p2p 有图片

动静分离成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值