Nginx.conf 配置小结

来源:互联网 发布:php网站架构 编辑:程序博客网 时间:2024/05/23 19:13

最近几个月的项目都是在nginx 环境下搭建的,特此记录下配置文件的相关内容,以备不时之需,欢迎吐槽~~

详细配置信息请勤奋查阅官方文档:http://wiki.nginx.org/Configuration


#指定Nginx运行的用户和用户组,据此可设置nginx访问文件夹的权限,防止非法用户访问无权限文件夹。顺便提一句,对于php工程的日志文件读写
#所属用户不是nginx,因为nginx是将php转发给php-cgi来解析,因此用户是php-cgi的所有者,一般是www-data
user nginx nginx;

#开启nginx的进程数目,数目应小于等于CPU总核心数,提高程序的并发性
worker_processes 4;
 
#全局访问和错误日志及其级别,[ debug | info | notice | warn | error | crit ]
access_log /var/nginx/access.log;
error_log /var/nginx/error.log warn;
#默认的日志格式设定
log_format access $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';

#进程运行时的文件
pid /run/nginx.pid;
 
#工作模式与连接数上限
events
{
#事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
# epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数),我的配置是768*4
worker_connections 768;
#开启同时接受多个请求,高并发
multi_accept on;
}
 
#设定http服务器
http
{
include mime.types; #使用MIME格式,文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,文件流
#charset utf-8; #默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
#设定上传文件的大小上限为8MB
client_max_body_size 8m; 

#开启高效文件传输模式,调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,#可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载和uptime。注意:如果图片显示不正常把这个改成off。
sendfile on;

#设置访问黑白名单, 文件名,大小
 white_black_list_conf conf/white.list zone=white1:4m;
 white_black_list_conf conf/black.list zone=black1:4m;
 white_list white1 on; #白名单 white1 在整个http{} 中都开启
 black_list black1 on; #黑名单 black1 在整个http{} 中都开启

tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 65; #长连接超时时间,单位是秒
 
#开启gzip,GNU 压缩静态资源,优化网站访问速度
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #开启压缩的文件大小下限
gzip_buffers 4 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流,一次申请4*16=64k缓存
gzip_http_version 1.0; #压缩使用http1.0及以上的文件
gzip_comp_level 2; #压缩等级,1-10,越大压缩率越高
#压缩文件类型,css,js,php,jpg,png
gzip_typestext/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png
gzip_vary on; #允许例外
gzip_disable"MISE[1-6]"#IE6及以下禁用压缩
#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用
 
upstream www.xxx.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;
}

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#各服务器的配置
server
{
#默认监听http 80端口, both ipv4 & ipv6
listen 80;
listen [::]:80 default_server ipv6only=on;
#域名可以有多个,用空格隔开
server_name www.xx.com xx.com;
index index.php;
root /data/www/xx;
#日志文件
 access_log  /usr/share/nginx/logs/pc_access.log;
 error_log  /usr/share/nginx/logs/pc_error.log;

#手机端导向另一个页面
 location / {
#判断访问终端类型
        if ($http_user_agent ~* '(Android|iPhone|iPad|webOS|iPod|BlackBerry)') {
                rewrite ^.+ http://m.xx.com;
        }
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }
#静态文件直接访问 
#图片缓存时间设置, 类似于html页面meta标签,可设置expire or max-age
location ~ \.(html|htm|gif|jpg|jpeg|png|bmp|swf)$ {
expires 10d;
                root /usr/share/nginx/html;
       }
#php导向9000 端口
location ~ \.php$ {
                root           /usr/share/nginx/html;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini


                # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }


#JS和CSS缓存时间设置
location ~ .*.(js|css)?$
{
expires 1h;
}
#错误页面
 error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }
#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
 
#如果使用j2ee, 可将页面导向8080端口,所有jsp的页面均交由tomcat或resin处理
location ~  \.(jsp|jspx|do|action)?$ {
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;
}
}

#再设置一个移动端访问的server, 接过手机端访问主站导来的请求
 server {
        listen       80;
        server_name m.xx.com www.m.xx.com;

        access_log  /usr/share/nginx/logs/mobile_access.log;
        error_log  /usr/share/nginx/logs/mobile_error.log;

        location / {
            root  /usr/share/nginx/html/mobile;
            index  index.html index.htm;
        }
        location ~ \.(html|htm|gif|jpg|png)$ {
                root /usr/share/nginx/html/mobile;
        }

        location ~ \.php$ {
                root  /usr/share/nginx/html/mobile;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}


0 0
原创粉丝点击