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

你可能感兴趣的:(服务器)