Nginx. conf configuration description
Detailed description of user www;
Define users and groups for running Nginx
Worker_processes 8; # [debug | info | notice | warn | error | crit]
Error_log/data1/logs/nginx_error.log crit; pid
/Usr/local/webserver/nginx. pid; # Specifies the value for maximum file descriptors that can be opened by this process.
Worker_rlimit_nofile 65535;
The maximum number of file descriptors opened by an nginx process. The theoretical value is the maximum number of opened files (ulimit
-N) the number of nginx processes is different from that of the nginx allocation request. Therefore, it is best to keep the value consistent with that of ulimit-n.
# Use [kqueue | rtsig | epoll |/dev/poll | select | poll];
Events use epoll; refer to the event model
Worker_connections 65535; Maximum number of connections per process (maximum connection = number of connections x number of processes) # Set the http server
Http include
Mime. types; file extension and file type ing table
Default_type application/octet-stream; # default file type
# Charset gb2312; default encoding
Server_names_hash_bucket_size 128; # size of the hash table with the server name
Client_header_buffer_size 32 k; size limit of uploaded files
Large_client_header_buffers 4 32 k; Set Request Delay
Client_max_body_size 8 m; Set Request Delay
Sendfile on; # enable the efficient File Transfer Mode
Tcp_nopush
On; prevents network congestion
Tcp_nodelay on; prevents network congestion
Keepalive_timeout 60; timeout
# FastCGI is designed to improve website performance-reduce resource usage and improve access speed.
For more information, see: http://www.fastcgi.com
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 k;
Fastcgi_buffers 4 64 k;
Fastcgi_busy_buffers_size 128 k;
Fastcgi_temp_file_write_size 128 k;
Gzip on;
Gzip_min_length 1 k; # minimum compressed file size
Gzip_buffers
4 16 k; # compression Buffer
Gzip_http_version 1.0;
# Compressed version (1.1 by default, 1.0 is used for squid2.5 at the front end
Gzip_comp_level 2; compression level
Gzip_types
Text/plain application/x-javascript text/css application/xml;
Compression type. text/html is already included by default, so you don't need to write any more below.
There will be no problem, but there will be a warn
Gzip_vary on;
# Limit_zone crawler $ binary_remote_addr 10 m; server listen 80;
Server_name www.opendoc.com.cn
Index index.html index.htm index. php;
Root/data0/htdocs/opendoc;
Location ~ . * \. (Php | php5 )? $ # Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf; # cache Images
Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $ expires 30d; # cache JavaScript CSS
Location ~ . * \. (Js | css )? $ Expires 1 h; # Log Settings
Log_format access' $ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer" ''" $ http_user_agent "$ http_x_forwarded_for ';
# Log format
Access_log/data1/logs/access. log access;
}
How does nginx determine mobile device users?
There are two methods
One is to use a language for judgment, for example, using php's $ _ SERVER ['user-agent']
<?php
function is_mobile(){
// returns true if one of the specified mobile browsers is detected
$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
/*
allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests:
include likes to both versions of the site w/ the special force mode parameters, 'mobile' and 'full':
<ahref="View'>http://www.php100.com/?mobile">View Mobile Site</a>
<ahref="View'>http://www.php100.com/?full">View Full Site</a>
Always check for 'mobile' or 'full' parameters before accounting for any User-Agent conditions:
*/
if ($_GET['mobile']) {
$is_mobile = true;
}
if ($_GET['full']) {
$is_mobile = false;
}
if($is_mobile) {
//it's a mobile browser, do something
header("Location: http://wap.baidu.com");
} else {
//it's not a mobile browser, do something else
header("Location: http://www.baidu.com");
// or instead of a redirect, simply build html below
}
?>
Another method is to use nginx to determine
If ($ http_user_agent ~ * (Mobile | nokia | iphone | ipad | android | samsung | htc | blackberry )){
// Add the statements to be processed, such as rewrite.
}
Some devices may not be identified. You can view the analysis log and write the keywords of User-Agent to if ~
The if statement of the nginx configuration file does not support multiple conditions such as "and" or. In some cases, we need the if statement to judge multiple conditions. How can we achieve this? We can use the set Statement of nginx to set variables.
Suppose we need to rewrite the/123/path, but at the same time we want to exclude that/123/images/path does not rewrite the path, we can use the following solution:
set $doRewrite "0";
if ($request_uri ~ ^/123/) {
set $doRewrite "1";
}
if ($request_uri ~ ^/123/images/) {
set $doRewrite "0";
}
if ($doRewrite = "1") {
// do rewrite
}
There is also an instance
This means to determine the real ip address, and then perform some operations based on the ip address ~ Map ing used here
map $http_x_forwarded_for $deny_access {
default 0;
1.2.3.4 1;
1.2.3.5 1;
1.2.3.6 1;
}
if ($deny_access = 1) {
return 403;
}
Anti-leech Configuration
location ~* \.(gif|png|jpg|bmp|swf|flv)$ {
valid_referers none blocked www.ruifengyun.com ruifengyun.com;
if ($invalid_referer) {
return 403;
}
}
In the preceding example, URLs with extensions of gif, png, jpg, bmp, swf, and flv can be used to prevent leeching. If you need other URLs to prevent leeching, add the corresponding suffix.
You can also replace return 403 with # rewrite ^/http://ruifnegyun.com/404.jpg; to promote your website in another way.
Nginx speed limit rules
Simple configuration, with only three lines
http{
……
limit_zone one $binary_remote_addr 10m;
……
server {
location / {
……
limit_conn one 2;
limit_rate 40k;
}
}
}
Limit_zone defines a container for each IP address to store the session status. In this example, a 10 m container named one is defined, and this name will be used in limit_conn. Limit_conn specifies that each visitor can only establish two links, and limit_rate limits the speed of each link to no more than 40 K. Therefore, the above configuration limits the total access speed of users to this site to 80 K.
This article is from "Fengyun, it's her ." Blog, please be sure to keep this source http://rfyiamcool.blog.51cto.com/1030776/1167837