nginx 反向代理做cache配置

1 2 3 4 5 6 7 8 9 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#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;
 
    server_names_hash_bucket_size 128;     #指定服务器名称哈希表的框大小
 
    client_header_buffer_size 32k;
    large_client_header_buffers 4 128k;     #以上两个是设定客户端请求的Header头缓冲区大小,对于cookie内容较大的请求,应增大改值。(400或414错误)
    client_max_body_size 8m;                #允许客户端请求的最大单文件字节数
    client_body_buffer_size 32k;            #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
 
    proxy_connect_timeout 600;              #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_read_timeout    600;              #连接成功后,后端服务器响应时间(代理接收超时)
    proxy_send_timeout    600;              #后端服务器数据回传时间(代理发送超时)
    proxy_buffer_size     32k;              #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers         4 32k;            #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size  64k;           #高负荷下缓冲大小(proxy_buffers*2)
    proxy_temp_file_write_size  1024m;      #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
    proxy_ignore_client_abort on;           #不允许代理端主动关闭连接
 
    sendfile           on;
    tcp_nopush         on;
    keepalive_timeout      65;
    tcp_nodelay     on;
    gzip         on;
    gzip_min_length      1k;
    gzip_buffers         4 16k;
    gzip_http_version     1.0;
    gzip_proxied         any;  #前端是squid的情况下要加此参数,否则squid上不缓存gzip文件
    gzip_comp_level     2;
    gzip_types        text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
 
    server_tokens off;
 
    #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
    proxy_temp_path   /cache/proxy_temp_path;
    #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
    proxy_cache_path  /cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
 
    server {
        listen       8012;
        server_name  localhost.net;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /var/www/nginx;
            index  index.html index.htm;
        }
 
        #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$ {
            proxy_pass  http://127.0.0.1:8011;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #include        fastcgi_params;
        }
 
    #扩展名以.gif、.jpg、.css等结尾的静态文件缓存。
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|mp3|mp4|flv|f4v|wmv|wma|mov)$
 
        {
            #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
 
        proxy_cache cache_one;                   #进行缓存,使用Web缓存区cache_one
        proxy_cache_valid 200 304 12h;           #对不同的HTTP状态码设置不同的缓存时间
        proxy_cache_valid 301 302 1m;
        proxy_cache_valid any 1m;
        proxy_cache_key $host$uri$is_args$args;  #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Accept-Encoding "none";  #设定proxy_set_header Accept-Encoding '';
                                                  #      (或是后台服务器关闭gzip),这样这台机器才
                                                  #       不会缓存被压缩的文件,造成乱码
        #proxy_set_header Accept-Encoding ""; #这个也可
        proxy_ignore_headers "Cache-Control" "Expires"; #这段配置加上后,proxy_cache就能支持后台设定的expires。
        #proxy_pass http://127.0.0.1:8011; #反向代理
        root   /var/www/nginx;
        expires  1h;
 
        }
    #设置只允许指定的IP或IP段才可以清除URL缓存。
        location ~ /purge(/.*)  {
            allow            127.0.0.1;
            allow            192.168.0.0/255;
            allow             all;
            proxy_cache_purge cache_one $host$1$is_args$args;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    #测试不同域名绑定同一端口
    server {
        listen       8012;
        server_name  localhost.com;
        location / {
            root   /var/www/nginx2;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            proxy_pass  http://127.0.0.1:8011;
        }
    }
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
 
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
 
    #    ssl_session_timeout  5m;
 
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}