nginx配置文件

本文介绍了一个在CentOS7环境下针对Nginx的高性能配置方案,包括了针对系统资源限制的配置调整、GZIP压缩设置、代理配置等关键细节。

环境

系统:centos7 物理:1G,1核,1M

配置

user nobody nobody;
worker_processes  1;

worker_rlimit_nofile 65535;

events {
    #使用epoll(异步非阻塞)的I/O模型
    use epoll;
    #每个进程的允许的最大连接数,理论上nginx服务器的最大连接数为worker_processes*worker_connections
    worker_connections  1024;
}

http {
    #以客户端IP地址为对象创建变量,用于创建IP白名单、黑名单等 
    #http://nginx.org/en/docs/http/ngx_http_geo_module.html
    #geo $geo {
     #   default        0;

    #    127.0.0.1      2;
    #    192.168.1.0/24 1;
    #    10.1.0.0/16    1;
   
    #    ::1            2;
    #    2001:0db8::/32 1;
    #}
    #创建一个依赖于其他变量的新变量,配合geo使用
    #http://nginx.org/en/docs/http/ngx_http_map_module.html
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    #关于mine.type:http://www.nosa.me/2014/10/30/
    include       mime.types;
    default_type  application/octet-stream;
    
    #定义日志内容格式
    log_format main '$remote_addr - - [$time_local] "$request" $status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" [$request_time, $upstream_response_time] $host ($remote_port) $scheme "sid=$cookie_sessionid"';
    
    #定义访问日志存储位置以及日志类型
    access_log  /var/log/nginx/access.log  main;
    
    #定义错误日志存储位置以及日志级别,级别由低到高debug, info, notice, warn, error, crit默认为crit 
    #error_log /var/log/nginx/error.log crit; #为了方便区分,到server内设置
    
    #隐藏版本号==》安全
    server_tokens off;

    #启动高效传输文件的模式
    sendfile        on;
   
    #这个选项仅在将连接转变为长连接的时候才被启用
    #作用是对小数据包进行收集后统一发送,避免资源浪费
    #https://www.cnblogs.com/wajika/p/6573014.html
    #https://www.cnblogs.com/felixzh/p/6283821.html
    tcp_nodelay     on;
    
    #延迟关闭,避免缓存区的tcp请求在停止服务时被强制关闭
    #http://blog.youkuaiyun.com/wangpengqi/article/details/17245889
    lingering_close off;
    
    #客户端链接超时时间,请求将在这个时间后关闭,避免资源无辜占用   
    keepalive_timeout 30;
    
    #发送超时,在连续的写操作过程中如果超时,连接暂时关闭
    send_timeout 60;
    #proxy_connect_timeout 60;
    #proxy_read_timeout 60;
    #proxy_send_timeout 60;
    
    #nginx状态码499问题http://www.nosa.me/2014/12/08/
    #proxy_ignore_client_abort on;
    
    # 最大上传包
    client_max_body_size 8m;
    
    ## GZIP Setting
    gzip  on;
    gzip_min_length  1000;
    gzip_buffers     4 8k;
    gzip_http_version  1.0;
    gzip_comp_level  5;
    gzip_types       text/plain text/css application/x-javascript application/json application/xml;
    
    #使用include管理每个Vhost
    include /usr/local/nginx/conf/servers/*/upstream.conf;
    include /usr/local/nginx/conf/servers/*/site.conf;
}

一个完整的nginx代理配置

  • 方法1: 使用include和upstream参数
[root@host2 test]# tail -n 5 /usr/local/nginx/conf/nginx.conf
    #使用include管理每个Vhost
    include /usr/local/nginx/conf/servers/*/upstream.conf;
    include /usr/local/nginx/conf/servers/*/site.conf;
}

[root@host2 test]# cat upstream.conf 
#配置上游服务器组“test”
upstream test {
    server 192.168.228.130;
}

[root@host2 test]# cat site.conf 
server {
    #配置本地端口及server_name
    listen 80;
    server_name 192.168.228.129;
    
    #配置被代理的服务器
    location /
    {
        proxy_pass      http://test;  #调用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;
    }
}

注意: 如果配置中使用的不是nginx自带变量,需要配置变量文件,并在调用过程中声明。

  • 使用nginx代理uwsgi服务
配置uwsgi变量文件:
[root@host2 test]# cat uwsgi_params
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;
uwsgi_param  UWSGI_FASTROUTER_KEY online_med;

创建uwsgi上游代理服务器组:
upstream uwsgi_admin {
    server 192.168.33.32:3630;
}

调用uwsgi变量:
location ~ ^/admin/ {
    uwsgi_pass uwsgi_admin;
    include uwsgi_params;  #声明uwsgi变量
    uwsgi_param  UWSGI_FASTROUTER_KEY online_med_admin;
}
  • 方法2:普通代理服务器配置
[root@adailinux vhost]# vim proxy.conf
server
{
    listen 80;
    server_name ask.apelearn.com;
    #定义域名(一般和被代理ip的域名保持一致)
    location /
    {
        proxy_pass      http://121.201.9.155/;
        #指定被代理(被访问)的IP(web服务器IP)
        proxy_set_header Host   $host;
        #$host指的是代理服务器的servername(也是被代理IP的域名)
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

转载于:https://my.oschina.net/adailinux/blog/1596685

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值