user nginx nginx; # 定义nginx的启动用户，不建议使用root

error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

worker_rlimit_nofile 65535; # 这个值为nginx的worker进程打开的最大文件数，如果不配置，会读取服务器内核参数（通过ulimit -a查看），如果内核的值设置太低会让nginx报错（too many open file），但是在此设置后，就会读取自己配置的参数不去读取内核参数

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    use epoll; # 客户端线程轮询方法、内核2.6版本以上的建议使用epoll
    worker_connections 65535; # 设置一个worker可以打开的最大连接数
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   120;
    types_hash_max_size 2048;
	
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
	
    server_tokens       off; # 隐藏nginx版本号
	
    proxy_intercept_errors on;
    fastcgi_intercept_errors on;
    fastcgi_connect_timeout 1300;
    fastcgi_send_timeout 1300;
    fastcgi_read_timeout 1300;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 4 512k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 512k;
 
    proxy_connect_timeout      20s;
    proxy_send_timeout         30s;
    proxy_read_timeout         30s;

    # 开启gzip
    gzip  on;
    # 启用gzip压缩的最小文件，小于设置值的文件将不会压缩
    gzip_min_length 1k;
    # gzip 压缩级别，1-10，数字越大压缩的越好，也越占用CPU时间。一般设置1和2
    gzip_comp_level 2;
    # 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    # 是否在http header中添加Vary: Accept-Encoding，建议开启
    gzip_vary on;
    # 禁用IE 6 gzip
    gzip_disable "MSIE [1-6]\.";
    # 设置缓存路径并且使用一块最大100M的共享内存，用于硬盘上的文件索引，包括文件名和请求次数，每个文件在1天内若不活跃（无请求）则从硬盘上淘汰，硬盘缓存最大10G，满了则根据LRU算法自动清除缓存。
    proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=10g;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /usr/local/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

server {
    listen 80;
    server_name 192.168.10.20;
    access_log off;
 
    location / {
        proxy_pass http://192.168.10.20:8080;
        # 隐藏端口号
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size  10m;
	#root /home/power/maintain/;
	#index index.html;
    }

    # 需添加项目所需的静态资源的后缀名，根据项目需要进行添加
    location ~ .*\.(css|js|ico|gif|jpg|jpeg|png|otf|eot|svg|ttf|woff|woff2|htm|mp3|json|scss)$ {
        log_not_found off;
	expires 7d; 
	access_log off;
	root /home/power/static/;
	proxy_store on; 
	proxy_temp_path    /home/power/static/;
	proxy_redirect     off;  
	proxy_set_header   Host 192.168.10.20:8080;  
	client_max_body_size  10m;
	client_body_buffer_size 1280k;  
        proxy_connect_timeout  900;  
        proxy_send_timeout   900;  
        proxy_read_timeout   900;  
        proxy_buffer_size    40k;  
        proxy_buffers      40 320k;  
        proxy_busy_buffers_size 640k;  
        proxy_temp_file_write_size 640k;  
        if ( !-e $request_filename)  
	{  
	     proxy_pass http://192.168.10.20:8080;
	}
	# 缓存过期管理
	proxy_cache_valid 200 302 1d;
	proxy_cache_valid 404 10m;
	proxy_cache_valid any 1h;
	proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
    }
}

}