About some nginx advanced extended applications

Source: Internet
Author: User

Nginx. conf configuration description

 
 
  1. Detailed description of user www;

  2. Define users and groups for running Nginx

  3. Worker_processes 8; # [debug | info | notice | warn | error | crit]

  4. Error_log/data1/logs/nginx_error.log crit; pid

  5. /Usr/local/webserver/nginx. pid; # Specifies the value for maximum file descriptors that can be opened by this process.

  6. Worker_rlimit_nofile 65535;

  7. The maximum number of file descriptors opened by an nginx process. The theoretical value is the maximum number of opened files (ulimit

  8. -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.

  9. # Use [kqueue | rtsig | epoll |/dev/poll | select | poll];

  10. Events use epoll; refer to the event model

  11. Worker_connections 65535; Maximum number of connections per process (maximum connection = number of connections x number of processes) # Set the http server

  12. Http include

  13. Mime. types; file extension and file type ing table

  14. Default_type application/octet-stream; # default file type

  15. # Charset gb2312; default encoding

  16. Server_names_hash_bucket_size 128; # size of the hash table with the server name

  17. Client_header_buffer_size 32 k; size limit of uploaded files

  18. Large_client_header_buffers 4 32 k; Set Request Delay

  19. Client_max_body_size 8 m; Set Request Delay

  20. Sendfile on; # enable the efficient File Transfer Mode

  21. Tcp_nopush

  22. On; prevents network congestion

  23. Tcp_nodelay on; prevents network congestion

  24. Keepalive_timeout 60; timeout

  25. # FastCGI is designed to improve website performance-reduce resource usage and improve access speed.

  26. For more information, see: http://www.fastcgi.com

  27. Fastcgi_connect_timeout 300;

  28. Fastcgi_send_timeout 300;

  29. Fastcgi_read_timeout 300;

  30. Fastcgi_buffer_size 64 k;

  31. Fastcgi_buffers 4 64 k;

  32. Fastcgi_busy_buffers_size 128 k;

  33. Fastcgi_temp_file_write_size 128 k;

  34. Gzip on;

  35. Gzip_min_length 1 k; # minimum compressed file size

  36. Gzip_buffers

  37. 4 16 k; # compression Buffer

  38. Gzip_http_version 1.0;

  39. # Compressed version (1.1 by default, 1.0 is used for squid2.5 at the front end

  40. Gzip_comp_level 2; compression level

  41. Gzip_types

  42. Text/plain application/x-javascript text/css application/xml;

  43. Compression type. text/html is already included by default, so you don't need to write any more below.

  44. There will be no problem, but there will be a warn

  45. Gzip_vary on;

  46. # Limit_zone crawler $ binary_remote_addr 10 m; server listen 80;

  47. Server_name www.opendoc.com.cn

  48. Index index.html index.htm index. php;

  49. Root/data0/htdocs/opendoc;

  50. Location ~ . * \. (Php | php5 )? $ # Fastcgi_pass unix:/tmp/php-cgi.sock;

  51. Fastcgi_pass 127.0.0.1: 9000;

  52. Fastcgi_index index. php;

  53. Fcinclude gi. conf; # cache Images

  54. Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $ expires 30d; # cache JavaScript CSS

  55. Location ~ . * \. (Js | css )? $ Expires 1 h; # Log Settings

  56. Log_format access' $ remote_addr-$ remote_user [$ time_local] "$ request "'

  57. '$ Status $ body_bytes_sent "$ http_referer" ''" $ http_user_agent "$ http_x_forwarded_for ';

  58. # Log format

  59. Access_log/data1/logs/access. log access;

  60. }

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']

 
 
  1. <?php

  2. function is_mobile(){  

  3.     // returns true if one of the specified mobile browsers is detected  

  4.     $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";  

  5.     $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";  

  6.     $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";      

  7.     $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";  

  8.     $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";  

  9.     $regex_match.=")/i";          

  10.     return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));  

  11. }  

  12. /*  

  13. allow the user a way to force either the full or mobile versions of the site - use a GET parameter on requests:  

  14. include likes to both versions of the site w/ the special force mode parameters, 'mobile' and 'full':  

  15. <ahref="View'>http://www.php100.com/?mobile">View Mobile Site</a>

  16. <ahref="View'>http://www.php100.com/?full">View Full Site</a>

  17. Always check for 'mobile' or 'full' parameters before accounting for any User-Agent conditions:  

  18. */  

  19. if ($_GET['mobile']) {  

  20.  $is_mobile = true;  

  21. }  

  22. if ($_GET['full']) {  

  23.  $is_mobile = false;  

  24. }  

  25. if($is_mobile) {  

  26.     //it's a mobile browser, do something  

  27.     header("Location: http://wap.baidu.com");  

  28. } else {  

  29.     //it's not a mobile browser, do something else  

  30.     header("Location: http://www.baidu.com");  

  31.     // or instead of a redirect, simply build html below  

  32. }  

  33. ?>

Another method is to use nginx to determine

 
 
  1. If ($ http_user_agent ~ * (Mobile | nokia | iphone | ipad | android | samsung | htc | blackberry )){

  2. // Add the statements to be processed, such as rewrite.

  3. }

 

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:

 
 
  1. set $doRewrite "0"; 

  2. if ($request_uri ~ ^/123/) { 

  3. set $doRewrite "1"; 

  4. if ($request_uri ~ ^/123/images/) { 

  5. set $doRewrite "0"; 

  6. if ($doRewrite = "1") { 

  7. // 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

 
 
  1. map $http_x_forwarded_for $deny_access { 

  2.     default     0; 

  3.     1.2.3.4     1; 

  4.     1.2.3.5     1; 

  5.     1.2.3.6     1; 

  6. if ($deny_access = 1) { 

  7.     return 403; 

 

Anti-leech Configuration

 
 
  1. location ~* \.(gif|png|jpg|bmp|swf|flv)$ { 

  2.     valid_referers none blocked www.ruifengyun.com ruifengyun.com; 

  3.     if ($invalid_referer) { 

  4.             return 403; 

  5.     } 

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

 
 
  1. http{ 

  2.     …… 

  3.     limit_zone one $binary_remote_addr 10m; 

  4.     …… 

  5.     server { 

  6.         location / { 

  7.             …… 

  8.             limit_conn one 2; 

  9.             limit_rate 40k; 

  10.         } 

  11.     } 

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

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.