nginx靜態文件配置


一、nginx概念(反向代理,負載均衡,動靜分離)


1)反向代理

  • 正向代理:在客戶端配置代理服務器,通過代理服務器進行互聯網訪問
  • 反向代理:反向代理服務器就相當於目標服務器,即用戶直接訪問反向代理服務器就可以獲得目標服務器的資源

2)負載均衡

  • 負載均衡:增加服務器數量,然后將請求通過nginx分發到各個服務器,以達到負載均衡分配
  • 效果:在瀏覽器輸入localhost地址,負載均衡會均衡分配到各個端口

3)動靜分離

  • 動靜分離:為了加速網站的解析速度,可以把動態頁面和靜態頁面分別
  • 放到不同的服務器進行解析,加速並降低單邊服務器的壓力

二、default.conf文件配置

1)默認寫法

server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
    location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
}

2)靜態文件服務器*

server {
    listen       80;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    location /static {
        alias   /data;
        autoindex on;
    }
}

3)負載均衡

upstream testServe {
        server 127.0.0.1:8000 weight=3;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
}
server {
        listen 80;
        localhost / {
                proxy_pass http://testServe;
        }
}

4)修改請求信息,轉發到目標路徑

server {
    listen  8080;
    location / {
        proxy_pass http://42.192.139.14:8080;
    }
    location /rewrite {
        if ($uri = /rewrite) {
            rewrite /rewrite http://42.192.139.14:8080 break;
        }
        rewrite /rewrite/([^/]*)/(.*) http://42.192.139.14:8080/$1/$2 break;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM