Nginx動態和靜態分離配置

思路
nginx的動靜分離是通過nginx將動態和靜態資源的訪問進行分離
如果請求URL含有/resources 則將請求轉發到靜態資源的目錄下,如果訪問的URL不含有/resources 認爲請求爲動態請求,則進行反向代理轉發到動態服務器。

nginx.conf 靜態路徑配置

location /resources/  {
    alias /usr/local/openresty/nginx/html/resources/;
    index index.html index.html;
}

Nginx反向代理+靜態資源配置

完整的nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
        worker_connections 1024;
}
http {
        include    /usr/local/openresty/nginx/conf/mime.types;

        upstream backend_server {
                server 10.0.0.133:8080 weight=1;
                server 10.0.0.133:8090 weight=1;
        }


        server{
                listen 80;
                server_name localhost;

                location /resources/  {
                        alias /usr/local/openresty/nginx/html/resources/;
                        index index.html index.html;
                }
                location / {
                        proxy_pass http://backend_server ;
                        proxy_set_header Host $host;
                        proxy_set_header        X-Real-IP       $remote_addr;
                        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章