8、配置nginx
将nginx初始配置文件备份,我们要重新创建配置文件
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vi /usr/local/nginx/conf/nginx.conf
- user www www;
- worker_processes 4; # 工作进程数,为CPU的核心数或者两倍
- error_log logs/error.log crit; # debug|info|notice|warn|error|crit
- pid logs/nginx.pid;
- events {
- use epoll; #Linux最常用支持大并发的事件触发机制
- worker_connections 65535;
- }
- http {
- include mime.types; #设定mime类型,类型由mime.type文件定义
- 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"';
- access_log logs/access.log main;
- #设定请求缓冲
- server_names_hash_bucket_size 256; #增加,原为128
- client_header_buffer_size 256k; #增加,原为32k
- large_client_header_buffers 4 256k; #增加,原为32k
- #size limits
- client_max_body_size 50m; #允许客户端请求的最大的单个文件字节数
- client_header_timeout 3m;
- client_body_timeout 3m;
- send_timeout 3m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 120;
- tcp_nodelay on;
- server_tokens off; #不显示nginx版本信息
- limit_conn_zone $binary_remote_addr zone=perip:10m; #添加limit_zone,限制同一IP并发数
- #fastcgi_intercept_errors on; #开启错误页面跳转
- include gzip.conf; #压缩配置文件
- include proxy.conf; #proxy_cache参数配置文件
- include vhost/*.conf; #nginx虚拟主机包含文件目录
- include mysvrhost.conf; #后端WEB服务器列表文件
- }
cd /usr/local/nginx/conf/
mkdir vhost
vi proxy.conf
- proxy_temp_path /tmp/proxy_temp;
- proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g;
- client_body_buffer_size 512k; #原为512k
- proxy_connect_timeout 50; #代理连接超时
- proxy_read_timeout 600; #代理发送超时
- proxy_send_timeout 600; #代理接收超时
- proxy_buffer_size 128k; #代理缓冲大小,原为32k
- proxy_buffers 16 256k; #代理缓冲,原为4 64k
- proxy_busy_buffers_size 512k; #高负荷下缓冲大小,原为128k
- proxy_temp_file_write_size 1024m; #proxy缓存临时文件的大小原为128k
- #proxy_ignore_client_abort on; #不允许代理端主动关闭连接
- proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
vi mysvrhost.conf
- upstream cn100 {
- ip_hash; #会话保持
- server 192.168.0.115 max_fails=1 fail_timeout=60s;
- server 192.168.0.109 max_fails=1 fail_timeout=60s;
- }
vi gzip.conf
- #网页GZIP压缩设置
- #2012.4.2
- #可通过http://tool.chinaz.com/Gzips/检测压缩情况
- #
- #启动预压缩功能,对所有类型的文件都有效
- gzip_static on; #开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件
- #找不到预压缩文件,进行动态压缩
- gzip on;
- gzip_min_length 1k; #设置最小的压缩值,单位为bytes.超过设置的min_length的值会进行压缩,小于的不压缩.
- gzip_comp_level 3; #压缩等级设置,1-9,1是最小压缩,速度也是最快的;9刚好相反,最大的压缩,速度是最慢的,消耗的CPU资源也多
- gzip_buffers 16 64k; #设置系统的缓存大小,以存储GZIP压缩结果的数据流,它可以避免nginx频烦向系统申请压缩空间大小
- gzip_types text/plain application/x-Javascript text/css text/javascript;
- #关于gzip_types,如果你想让图片也开启gzip压缩,那么用以下这段吧:
- #gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
- #gzip公共配置
- gzip_http_version 1.1; #识别http的协议版本(1.0/1.1)
- gzip_proxied any; #设置使用代理时是否进行压缩,默认是off的
- gzip_vary on; #和http头有关系,加个vary头,代理判断是否需要压缩
- gzip_disable "MSIE [1-6]."; #禁用IE6的gzip压缩
vi vhost/cn100.conf
- server {
- listen 80;
- server_name localhost;
- #charset GB2312;
- location /
- {
- proxy_redirect off;
- 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://cn100;
- }
- # 查看nginx的并发连接数配置
- location /NginxStatus
- {
- stub_status on;
- access_log off;
- auth_basic "NginxStatus";
- }
- access_log off;
- error_page 404 /404.html;
- error_page 500 502 503 504 /404.html;
- location = /404.html {
- root html;
- }
- limit_conn perip 50; #同一ip并发数为50,超过会返回503
- }
