nginx 初学
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on ;
#负载平衡模块
upstream myreprot {
# server cnbjw2071:8089 weight=2 ;
# server cnbjpbg206:8090 weight=3 ;
server cnbjpbg272 ;
server cnbjpbg206;
}
#负载平衡模块
upstream nn {
server cnbjpbg272 ;
server cnbjpbg206 ;
}
server
{
listen 80;
server_name cnbjpbg206;
location / {
proxy_pass http://myreprot;
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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ { #指定缓存文件类型
expires 7d; #设置浏览器过期时间
root a; #所有的缓存文件都会保存在这里html等等,他还会缓存文件夹,所以不用担心覆盖,但是要注意时效性,不然你做了修改 他依旧读取缓存,你的网站就没有变化了
proxy_store on; #开启缓存机制
proxy_store_access user:rw group:rw all:rw; #缓存读写规则
proxy_temp_path b; #存放静态文件的缓存目录
#include proxy.conf; # 外联proxy理的详细配置如proxy_set_header, client_max_body_size ....
if ( !-e $request_filename) {
proxy_pass http://myreprot;
}
}
}
#直接访问 http#semcw6081:8096即可实现平衡负载 . #用.代替
server
{
listen 8096;
server_name SEMCW6081;
location / {
proxy_pass http://nn; #模块开发,只有和upstream 的名字统一就行.
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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ { #指定缓存文件类型
expires 7d; #设置浏览器过期时间
root a; #所有的缓存文件都会保存在这里html等等,他还会缓存文件夹,所以不用担心覆盖,但是要注意时效性,不然你做了修改 他依旧读取缓存,你的网站就没有变化了
proxy_store on; #开启缓存机制
proxy_store_access user:rw group:rw all:rw; #缓存读写规则
proxy_temp_path b; #存放静态文件的缓存目录
#include proxy.conf; # 外联proxy理的详细配置如proxy_set_header, client_max_body_size ....
if ( !-e $request_filename) {
proxy_pass http://nn;
}
}
}
}