NGINX 配置文件优化
· 技术积累 · Linux Nginx

参考文章
http://www.ha97.com/5194.html
http://www.cnblogs.com/dawnlee/p/5142724.html

#运行用户
user nginx;

#进程文件pid
pid pid/nginx.pid;

#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;

#优化worker processes
#nginx是多进程的而不是多线程的,对于进程相关的配置我们需要如下优化。首先看一下服务器的处理器数。
worker_processes 2;
worker_rlimit_nofile 100000;

#工作模式与连接数上限
events {

#单个进程最大连接数(最大连接数=连接数*进程数)   
    worker_connections 2048;
    multi_accept on; #必须打开

#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll;
}
http {
#引用其他配置文件
    include       mime.types;#文件扩展名与文件类型映射表
    include       conf.d/blog.conf;
    include       conf.d/bugfix.conf;
    include       conf.d/me.conf;
    charset utf-8; #默认编码
    fastcgi_intercept_errors on;  #开启自定义404页面

#日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';



#LOGS 禁用或者优化access_log大流量访问时,较大的访问会导致访问日志对磁盘的读写非常大。如果不需要日志的话,可以禁用掉。
#access_log off;
#log_not_found off;
#或者打开缓冲(定义访问日志)
access_log logs/access.log main buffer=32k;

client_header_timeout 300s;        #调大点
client_body_timeout 300s;          #调大点
client_max_body_size 100m;         #限制上传文件大小
client_body_buffer_size 256k;

tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #长连接超时时间,单位是秒

#其他
default_type text/html;
charset UTF-8;
server_tokens off; 
sendfile on; 
tcp_nopush on; 
tcp_nodelay on;
error_log logs/error.log crit; 
keepalive_timeout 10; 
client_header_timeout 10;
client_body_timeout 10; 
reset_timedout_connection on; 
send_timeout 10; 
#limit_conn_zone $binary_remote_addr zone=addr:5m; 
#limit_conn addr 100; 


#开启高效文件传输模式
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

#优化输出缓存 FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

#缓存
proxy_connect_timeout 60s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /tmp/nginx;
proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;



#负载均衡
upstream test.com {
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}



#禁止http ip访问
server
{
listen 80 default;
return 500;
}

#禁止http/https ip访问
server {
listen 80 default_server;
listen 443 ssl default_server;
server_name _;
ssl_certificate path_to_your_fullchain.cer;
ssl_certificate_key paht_to_your_key;
#rewrite ^ https://blog.wojc.cn; #自定义跳转url
return 100; #返回指定的状态
}

server {
#80 端口跳转443 端口   
    if ($server_port = 80){
    return 301 https://$server_name$request_uri;}

    if ($scheme = http){
    return 301 https://$server_name$request_uri;}
    error_page 497 https://$server_name$request_uri;


listen 80; #监听端口
listen 443 ssl; #开启ssl端口
server_name www.test.com test.com;#域名可以有多个,用空格隔开
index index.html index.htm index.php; #支持的首页类型
root /data/nginx/html; #网站根目录

#ssl 配置 (此处以腾讯云申请的证书给出的配置为例子)
      ssl_certificate www.test.com.crt;  #此文件为你申请的ssl证书
      ssl_certificate_key www.test.com.key;  #此文件为你申请的ssl key
      ssl_session_timeout 5m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
      ssl_prefer_server_ciphers on;

    client_header_buffer_size 2048k;
    large_client_header_buffers 4 2048k;

#对 "/" 启用反向代理
location / {
    proxy_pass http://test.com;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以下是一些反向代理的配置,可选。
    client_max_body_size 10m; #允许客户端请求的最大单文件字节数
    client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
    proxy_connect_timeout 90s; #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_send_timeout 600s; #后端服务器数据回传时间(代理发送超时)
    proxy_read_timeout 600s;
}

#开启php转发
     location ~ .*\.(php|php5)?$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /data/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }  
 
location / {
    root /data/nginx;
    index index.php;
     proxy_cache cache_one ;#使用Web缓存区cache_one,已在nginx.conf的缓存配置中命名的
     proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";#不处理后端服务器返回的指定响应头
    
     #对不同HTTP状态码缓存设置不同的缓存时间
         proxy_cache_valid 200 304 12h ;
         proxy_cache_valid 301 302 1m ;
         proxy_cache_valid any 1m ;
     #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名,URI,参数"组合成Key
         proxy_cache_key $host$uri$is_args$args;
}

#图片缓存时间设置
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        root /你的网站目录
        expires 10d;
    }
#JS和CSS缓存时间设置
    location ~ .*\.(js|css)?$ {   
        root /你的网站目录
        expires 1h;
    }


#使用cdn后 访问日志中获取真实ip
#set_real_ip_from *.*.*.*;  #前端主机的ip段或主机ip
#real_ip_header    X-Real-IP;
#real_ip_header    X-Forwarded-For;

#允许后台登录的ip
    location ^~ /admin {
        allow 61.171.93.119;
        deny all;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param        SCRIPT_FILENAME   /usr/local/nginx/html/admin$fastcgi_script_name;
            include        fastcgi_params ;
    }

    #对于下载目录中的文件大小使用Mb来显示,显示主机文件时间
    location ~* /novel/(lnmp|urban|doc|fantasy|thorugh) {
        #允许列出目录中内容
        autoindex on;

        #默认为off,显示的文件时间为GMT时间。
        #改为on后,显示的文件时间为文件的服务器时间
        autoindex_localtime on;

        #默认为on,显示出文件的确切大小,单位是bytes。
        #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
        autoindex_exact_size off;

#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
    location ~ .(jsp|jspx|do)?$ {
        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://127.0.0.1:8080;
    proxy_connect_timeout   60s;    
        proxy_send_timeout      600s;        
        proxy_read_timeout      600s;

    }

#所有静态文件由nginx直接读取不经过tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        { expires 15d; }

    location ~ .*.(js|css)?$
        { expires 1h; }}
    }
}

本文最后更新时间 2023-06-08
文章链接地址:
https://me.jinchuang.org/archives/18.html
本站文章除注明[转载|引用|原文]出处外,均为本站原生内容,转载前请注明出处


留言