一个简单的nginx配置

今天需要设置一下nginx的配置,作个记录。

要点:

  1. 一点优化
  2. http1.1连接
  3. 冷备
  4. 加了个监控
user nginx;  
worker_processes 4;  
worker_cpu_affinity 0001 0010 0100 1000; #cpu开启4个进程,是一个优化项。可以减少内存访问损耗,提高速度

error_log /var/log/nginx/error.log;#错误日志  
pid /var/run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {  
    use epoll;#epoll的I/O模型,不设置也会依据系统自己配置,内核2.6+就支持
    worker_connections 10240;#每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections
}

http {  
    log_format  main  '$remote_addr - $remote_user [$time_local]  $http_host "$request" '
                      '$status $upstream_addr  $upstream_status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time $upstream_response_time';
    # 这里面的request_time和upstream_response_time可以作为监控项,另外upstream_addr可以看到到底用的是哪个webservice

    access_log  /var/log/nginx/access.log  main;#访问日志地址

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    gzip                on;

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

    index   index.html index.htm;

    upstream yourserver {
        #ip_hash;
        keepalive 60;#必须加
        server 172.31.**.1:8080;
        server 172.31.**.2:8080 backup;#作为冷备,如果上面的挂了,会请求这个,上面的恢复后又不再使用这个
    }
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_http_version 1.1;#http1.1必加
                proxy_set_header Connection "";#http1.1必加
                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_connect_timeout 10;
                proxy_send_timeout 30;
                proxy_read_timeout 30;
                proxy_buffer_size 256k;
                proxy_buffers 4 256k;
                proxy_busy_buffers_size 256k;
                proxy_temp_file_write_size 256k;
                proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
                proxy_max_temp_file_size 128m;
                proxy_pass http://yourserver;

        }

        location /ngx_status {
                stub_status on;
                access_log off;
                allow yourip;#开放指定的ip访问
                deny all;#不允许别的客户端访问
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

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

监控页面:http://nginxhost:80/ngx_status, 监控页面中的含义:

Active connections: 45  
server accepts handled requests  
 4513 4513 6773 
Reading: 0 Writing: 2 Waiting: 43  
Active connections:现有多少个连接  
处理连接数 连接句柄数 处理了多少个请求
例如上面是意思是:总共处理了4513个连接,成功创建4513次握手,总共处理了6773个请求

Reading:读取到客户端的Header信息数目  
Writing: 正在返回给客户端的Header信息数  
waiting:等待中的数目,开启 keep-alive 的情况下,这个值等于active-(reading+writing),意思就是Nginx已经处理完正在等候下一次请求指令的驻留连  
comments powered by Disqus