关闭

nginx反向代理及高速缓存

标签: centosnginx缓存
298人阅读 评论(0) 收藏 举报
分类:

安装这里就不说了,可以参考我上一篇博客,下面直接上配置,关键点我会注释


以下是nginx.conf配置:

user  nginx;
worker_processes  4;    #worker的进程数,根据cpu数量调整

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 203568;
worker_rlimit_core 2000M;


events {
    worker_connections  201784;   #worker允许的最大并发数
}

http {
    include       mime.types;
    server_tokens off;
    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"';

    log_format main '$remote_addr - [$time_local] "$request_method $scheme://$http_host$request_uri TCP/$server_protocol" $status $bytes_sent "$http_referer" "$http_user_agent"';

    access_log  logs/access.log  main;   #日志产生位置

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  30;

  

resolver 127.0.0.1;

charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;

gzip on;    #开启压缩
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types   text/plain application/x-javascript text/css application/xml text/j    avascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;

proxy_temp_path /data/proxy_temp_dir;    #缓存缓冲区,  proxy_cache_path缓存磁盘大小深度时间配置
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;

upstream www.b.com {
   server 192.168.7.247:80 weight=1;   #改成你自己要代理的服务器地址
}

server {
        listen       80;
        server_name  www.test.com;  #这里换成你要代理的域名,多个域名用逗号分开。

        #charset koi8-r;

        server_tokens off;
        tcp_nopush on;
        sendfile on;

        location / {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache cache_one;
            proxy_cache_valid 200 304 12h;    #设置状态缓存的时间为12小时
            proxy_cache_key $host$uri$is_args$args;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://$host;
        }

  location ~ .*\.(gif|png|htm|css|js|flv|ico|swf)(.*) {
             proxy_redirect off;
             proxy_set_header Host $host;
             proxy_cache cache_one;
             proxy_cache_valid 200 302 12h;
             proxy_cache_valid 301 1d;
             proxy_cache_valid any 1m;
             proxy_cache_key $host$uri$is_args$args;       
             proxy_pass http://$host;
        }

error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

如果对上面的配置有不明白地方,可以加群讨论(284215611)

0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20360次
    • 积分:302
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档