NGINX.CONF Configuration Summary

Source: Internet
Author: User
Tags epoll php tutorial sendfile
in recent months, the project has been built under the NGINX environment, hereby record the relevant contents of the configuration file, in case of a rainy days, welcome to Vomit Groove ~ ~

Detailed configuration information please consult the official documentation: Http://wiki.nginx.org/Configuration

#指定Nginx运行的用户和用户组, you can set the permissions of the Nginx Access folder to prevent unauthorized users from accessing the folder without permissions. By the way, for PHP engineering log files Read and write

#所属用户不是nginx, because Nginx is the PHP forwarded to php-cgi to parse, so the user is the owner of php-cgi, generally www-data

User Nginx Nginx;

#开启nginx的进程数目, the number should be less than or equal to the total CPU core number, improve the concurrency of the program

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 model is a high-performance network I/O model in Linux 2.6 and above, and if running on FreeBSD, use the Kqueue model.

Use Epoll;

#单个进程最大连接数 (maximum number of connections = number of connections * processes), my configuration is 768*4

Worker_connections 768;

#开启同时接受多个请求, High concurrency

Multi_accept on;

}

#设定http服务器

http

{

Include Mime.types; #使用MIME格式, file name extension and file type mapping table

Default_type Application/octet-stream; #默认文件类型, File stream

#charset Utf-8; #默认编码

Server_names_hash_bucket_size 128; #服务器名字的hash表大小

#设定上传文件的大小上限为8MB

Client_max_body_size 8m;

#开启高效文件传输模式, call the Sendfile function (zero copy mode) to output the file , for the general application is set to ON, if used to download applications such as disk IO heavy load application, #可设置为off to balance the disk and network i/ o processing speed, reduce the load and uptimeof the system. Note: If the picture is not normal, change this to off.

Sendfile on;

#设置访问黑白名单, file name, size
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 is turned on throughout http{}.

Black_list Black1 on; #黑名单 Black1 is turned on throughout http{}.

Tcp_nopush on; #防止网络阻塞

Tcp_nodelay on; #防止网络阻塞

Keepalive_timeout 65; #长连接超时时间, Unit is seconds

#开启gzip, GNU compresses static resources to optimize website access speed

gzip on; #开启gzip压缩输出

Gzip_min_length 1k; #开启压缩的文件大小下限

Gzip_buffers 4 16k; # Set the system to get several units of cache used to store gzip compressed result data streams, one request 4*16=64k cache

Gzip_http_version 1.0; #压缩使用http1. Documents 0 and above

Gzip_comp_level 2; #压缩等级, 1-10, the higher the compression rate, the greater the

#压缩文件类型, 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 is a weight that can be defined based on machine configuration. The Weigth parameter represents weights, and the higher the weight, the greater the probability of being allocated.

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相关参数是为了改善网站的性能: Reduce resource usage and improve access speed.

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 ports, both IPv4 & IPv6

Listen 80;

Listen [::]:80 default_server Ipv6only=on;

#域名可以有多个, separated by a space

server_name www.xx.com xx.com;

Index index.php;

ROOT/DATA/WWW/XX;

#手机端导向另一个页面

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;
}
#静态文件直接访问

#图片缓存时间设置, similar to HTML page meta tags, can be set expire or max-age

Location ~ \. (Html|htm|gif|jpg| jpeg|png|bmp|swf) $ {

Expires 10d;
root/usr/share/nginx/html;
}

#php导向9000 Port

Location ~ \.php$ {
root/usr/share/nginx/html;
Fastcgi_split_path_info ^ (. +\.php) (/.+) $;
# note:you should has "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 502 503 504/50x.html;
Location =/50x.html {
root/usr/share/nginx/html;
}

#对 "/" Enable reverse Proxy

Location/{

Proxy_pass http://127.0.0.1:88;

Proxy_redirect off;

Proxy_set_header X-real-ip $remote _addr;

#后端的Web服务器可以通过X-forwarded-for Get the user real IP

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

#以下是一些反向代理的配置, optional.

Proxy_set_header Host $host;

Client_max_body_size 10m; #允许客户端请求的最大单文件字节数

Client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,

Proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间 (proxy connection timed out)

Proxy_send_timeout 90; #后端服务器数据回传时间 (proxy send timeout)

Proxy_read_timeout 90; #连接成功后, back-end server response time (proxy receive timeout)

Proxy_buffer_size 4k; #设置代理服务器 (nginx) buffer size for saving user header information

Proxy_buffers 4 32k; #proxy_buffers缓冲区, the average Web page is set below 32k

Proxy_busy_buffers_size 64k; #高负荷下缓冲大小 (proxy_buffers*2)

Proxy_temp_file_write_size 64k;

#设定缓存文件夹大小, greater than this value, will be passed from the upstream server

}

#设定查看Nginx状态的地址

Location/nginxstatus {

Stub_status on;

Access_log on;

Auth_basic "Nginxstatus";

Auth_basic_user_file confpasswd;

#htpasswd文件的内容可以用apache提供的htpasswd工具来产生.

}

#如果使用j2ee, the page is directed to port 8080, all JSP pages are processed by tomcat or 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, took the request from the mobile phone to access the master station

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

}


The above describes the NGINX.CONF configuration summary, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.