配置文件位置:nginx/conf/nginx.conf
upstream 127.0.0.1 {
server 127.0.0.1:80 weight=1;
#目标服务器1的ip地址和端口号 weight权重
#server 192.168.28.145:80 weight=1;
#目标服务器2的ip地址和端口号 weight权重
server 192.168.28.146:80 weight=1;
#目标服务器3的ip地址和端口号 weight权重
#server 192.168.28.136:80 weight=1;
}
server {
#监听端口号。访问nginx服务器88端口,会进行反向代理和负载均衡
listen 88;
#服务名称,此处填写如www.csdn.net
server_name 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
#代理ip地址,本机127.0.0.1
location / {
proxy_pass http://127.0.0.1;
}
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
proxy_pass http://127.0.0.1;
#root work;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
proxy_pass http://127.0.0.1;
#root work;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
在配置过程中会出现静态资源加载不完全的情况,这是因为nginx 需要缓存这些资源,如果nginx 缓存设置太小就会出现js文件加载不全的情况,可以通过如下配置,动态更改缓存设置:
proxy_buffer_size 512k;
proxy_buffers 64 512k;
proxy_busy_buffers_size 512k;
同时静态资源太多,可以采用静态资源缓存到nginx服务器上,其在http 中添加配置如下:
proxy_temp_path work;
proxy_cache_path work/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30g;