nginx实例实战

1.先下载一个,地址http://nginx.org/

启动命令start nginx 或 nginx.exe,浏览器输入localhost


2.反向代理

默认配置是查找nginx目录下的html
        location / {
            root   html;
            index  index.html index.htm;
        }
/  斜杠指的是,所有的流量都要经过这里
修改为
        location / {
            proxy_pass  https://localhost:8080;
            root   html;
            index  index.html index.htm;
        }


3.负载均衡
添加多台server 
    upstream demo{
        server 192.168.2.138:8080;
        server 192.168.2.138:8081;
    }
修改location为 
        location / {
            proxy_pass  https://demo;
            root   html;
            index  index.html index.htm;
        }


从现象可以看到
这样,来回切换服务器,会导致,session不共享,最简单的跨域解决办法
根据请求的ip地址,去取一个hash,根据ip地址的hash会将的请求发到同一台服务器
解决办法
    upstream demo{
        ip_hash;
        server 192.168.2.138:8080;
        server 192.168.2.138:8081;
    }

4.动静分离
增加配置
        只要是以jpg或者gif结尾的,去配置的目录下查找
        location ~ .*\.(jpg|gif)$ {
            root /usr/local/images;
        }


5.手机站点和PC站点的分离 
location / {
            if ($mobile_rewrite = 0) {
                proxy_pass http://localhost:8082;
            }
            proxy_pass   http://demo;
            root   html;
            index  index.html index.htm;
        }
6.站点增加则按照如下配置
upstream pc{
        ip_hash;
        server 192.168.2.138:8080;
        server 192.168.2.138:8081;
    }
    
upstream mobile{
        ip_hash;
        server 192.168.2.138:8082;
        server 192.168.2.138:8083;
    }

    location / {
            if ($mobile_rewrite = 0) {
                proxy_pass http://mobile;
            }
            proxy_pass   http://pc;
            root   html;
            index  index.html index.htm;
        }

要想深入,还需要精致的学习一下





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章