- 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;
- }
- }