nginx 负载服务器优化

713 查看

user  www www;

# ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右 
worker_processes  4;

# 为每个进程分配cpu,上例中将4 个进程分配到4个cpu,当然可以写多个,或者将一 个进程分配到多个cpu。
worker_cpu_affinity 00000001 00000010 00000100 00001000;

# 每个nginx进程打开文件描述符最大数目 配置要和系统的单进程打开文件数一
# 致,linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应,应该填写65535  
# nginx调度时分配请求到进程并不是那么的均衡,假如超过会返回502错误。我这里写的大一点
worker_rlimit_nofile 100000;

# 开启nginx错误日志
error_log  logs/error.log;

# 告诉nginx只能记录严重的错误
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    # 每个工作进程允许最大的同时连接数(Maxclient = work_processes * worker_connections)
    # 默认1024
    worker_connections  65535;
    
    # 告诉nginx收到一个新连接通知后接受尽可能多的连接。
    multi_accept on;
    
    # 设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。
    # 如果你使用*BSD,你应该使用kqueue。
    # 值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的
    use epoll;
}


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

    limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;


    # 
    #access_log  logs/access.log  main;

    # 这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,
    # inactive 是指经过多长时间文件没被请求后删除缓存
    open_file_cache max=204800 inactive=20s;
    
    # open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,
    # 如果超过这个数字,文件描述符一直是在缓存中打开的,
    # 如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除
    open_file_cache_min_uses 1;
    
    # 这个是指多长时间检查一次缓存的有效信息
    open_file_cache_valid 30s;

    # 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的
    server_tokens off;
    
    # 磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。
    # Pre-sendfile是传送数据之前在用户空间申请数据缓冲区
    sendfile        on;
    
    # 告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
    #tcp_nopush     on;
    
    # 告诉nginx不要缓存数据,而是一段一段的发送,
    # 当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
    #tcp_nodelay on;

    upstream phpServer{
        server 172.20.17.210:9000 weight=1 max_fails=2 fail_timeout=3;
        server 172.20.17.211:9000 weight=1 max_fails=2 fail_timeout=3;
    }

    # keepalive超时时间
    keepalive_timeout  65;
    client_max_body_size 2m;

    # 不准许IP直接访问, 直接访问报500错误
    server {
        listen 80 default_server;
        server_name _;
        return 500;
    }

    # 配置虚拟主机,过个server就复制多个
    server {
        listen 80;
    
        # 开启gzip压缩
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        #gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
        gzip_vary off;
        gzip_disable "MSIE [1-6]\.";
    
        # 配置域名
        server_name  www.xxxxx.com xxxxx.com;
    
        # 配置网站目录
        root   /usr/local/nginx/html/xxxxx.com;
    
        # 只允许我们的域名的访问
        if ($host !~ ^(xxxxx.com|www.xxxxx.com|images.xxxxx.com)$ ) {
            return 444;
        }
    
        # 配置域名重定向
        #if ($host != 'www.xxxxx.com' ) {
        #    rewrite ^/(.*)$ http://www.xxxxx.com/$1 permanent;
        #}
        
        # 限制可用的请求方法
        if ($request_method !~ ^(GET|HEAD|POST)$ ) {
            return 444;
        }
        
        # 如何拒绝一些User-Agents
        if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
            return 403;
        }
        
        # 如何防止图片盗链
        location /images/ {
            valid_referers none blocked www.xxxxx.com xxxxx.com;
            if ($invalid_referer) {
                return   403;
            }
        }
    
        location / {
            # 配置rewrite
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
            }
    
            # include  /usr/local/nginx/html/yphp/.htaccess;
            # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;
    
            # 配置默认访问文件
            index  index.php index.html index.htm;
        }
    
        # 包含虚拟主机公用配置文件
        include server.conf;
    }
}

php-fpm.conf 文件配置优化

[global]
pid = run/php-fpm.pid
process_control_timeout=5
[www]
listen.allowed_clients = 127.0.0.1
user=www
group=www
pm=dynamic

# 增加 PHP-FPM 打开文件描述符的限制
rlimit_files = 51200

# 这个配置决定了php-fpm的总进程数,内存小的少设点
pm.max_children=20

# 并发数越大,此请求数应越大
pm.max_requests=10000

# 初始php-fpm进程数
pm.start_servers =10

# 动态方式下的起始php-fpm进程数量(设置太大可能会报错,根据服务器配置设置)
pm.min_spare_servers = 5

# 动态方式下服务器空闲时最小php-fpm进程数量
pm.max_spare_servers = 10

# 表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi
# 进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
emergency_restart_threshold = 60
emergency_restart_interval = 60s

iptables 防火墙限制用户访问平率

# 下面的例子会阻止来自一个IP的60秒钟内超过15个连接端口80的连接数
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –set
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –update –seconds 60  –hitcount 15 -j DROP
service iptables save