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

要想深入,還需要精緻的學習一下





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