Nginx Learning Summary

Source: Internet
Author: User

Nginx High Performance HTTP and reverse proxy server
limitation: supports only HTTP and mail two types
Nginx uses an efficient network I/O model for different Linux distributions??? Epoll (Linux 2.6 kernel)??? kqueue (FreeBSD)??? Eventport (Solaris)
Nginx Server can support up to 50,000 concurrent connections in response, MEM,CPU resource consumption is low (actual production environment of four concurrent connections)
Nginx Advantage??? Config file Simple??? support Rewrite rewriteThe ability to divide HTTP requests into different backend servers based on domain names, URLs??? to support Check server functionality????? back end a Web server down that does not affect access??? Save Bandwidth????? support for gzip compression, can add browser local cache header header??? High stability??? support for hot deployment
nginx Installation and configuration
CentOS or RedhatYum Install Nginx
Ubuntuapt-get Install Nginx
local repo config/etc/yum.repos.d/nginx.repo[Nginx]
Name=nginx Repo
baseurl=http://nginx.org/packages/os/osrelease/$basearch/
Gpgcheck=0
Enabled=1


nginx configuration file
please refer to the following connection for detailed configuration file:http://wiki.nginx.org/Configuration
User www www; #主进程以root用户启动, then switch to the current option to configure users and group, default to nobody, the default is to only configure the username, then the group also uses the nameworker_processes? 2; ?? #一般为内核数量pid/var/run/nginx.pid;?? #PID文件位置, such as reloading the configuration? Kill-hup ' Cat/var/log/nginx.pid '?# [Debug | info | notice | warn | error | crit]?error_log?/var/log/nginx.error_log info;?? #error log location different from domain log registrations? ???????????????????????? section-error??????????????????????????????. #in the HTTP section-crit??????????????????????????????. #in the server Section-crit?? Events {??? #Nginx如何控制connectionworker_connections ???? #worker可以开启的连接数量?? # Use [Kqueue | rtsig | epoll |/dev/poll | select | poll];???The use Kqueue;?? #使用哪种IO网络模型, the default is based on the Configure operating system to determine Epoll in the Linux version is more commonly used? }
# # # Generally speaking?? Max clients = worker_processes * Worker_connections# # # reverse proxy? max clients = worker_processes * WORKER_CONNECTIONS/4
?? http {?? #核心HTTP Server Configuration??????. conf/mime.types;?? #可以引入其他的配置文件Default_type application/octet-stream,?? #指定MIME-type, normally without configuration??? Log_format main??? ' $remote _addr-$remote _user [$time _local]??? #日志格式 Main is the log format name, convenient for other location references, does not allow duplicates? ? ? "$request" $status $bytes _sent '? ? ? ' "$http _referer" "$http _user_agent" '? ? ? ' $gzip _ratio ';
? ? log_format download? ' $remote _addr-$remote _user [$time _local]? '? ? ? "$request" $status $bytes _sent '? ? ? ' "$http _referer" "$http _user_agent" '? ? ? ' "$http _range" "$sent _http_content_range";
???? # $remote _ADDR: Reverse proxy server address???? #? $remote _user: Remote client name???? #? [$time _local]: Access time and time zone????? $request: Record the URL and protocol of the request?????? $status: Record request status????? $body _bytes_sent: Client sends body Neisson size???? #?$ Http_referer: from which page link is accessed???? #? $http _user_agent: Record client browser related information??? # $http _x_forwarded_for: Record the original client IP address and original client Request server IP address??? client_header_timeout? 3m;?? #指定多长时间等待客户端发送一个请求头???. client_body_timeout. 3m;?? #从客户端读取body超时时间Send_timeout????. 3m;?? #指定为客户端响应超时时间????. client_header_buffer_size. 1k;?? #客户端headerbuffer大小?? large_client_header_buffers? 4 4k;?? #最大Headerbuffer size??? gzip on;?? #开启gzip压缩?? gzip_min_length 1100;?? #Content-lengthGreater than this length will be compressedgzip_buffers 4 8k;?? # Compress BufferGzip_types??????????.. #添加额外压缩类型, default includes text/html??? output_buffers? 1 32k;?? postpone_output 1460;?Sendfile??? #sendfile模式, more efficient, directly through kernel, without user spacethe Tcp_nopush, the #有点深奥, did not take the time to study and use it with Sendfile .?Tcp_nodelay??? on ;?? #同上?? Send_lowat??? 12000;????. #指定的超时时间, stay connected to the customer. Keepalive_timeout The server will close the connection after this time. ??? # Lingering_time??? # lingering_timeout;?? # Reset_timedout_connection? on;???? server {?? #虚拟服务器配置 based on IP and domain name-basedListen???????????.. #监听服务器, address and port, default bound 80 port??? server_name? one.example.com www.one.example.com;?? #访问的名称支持正则表达式?access_log ??????/var/log/nginx.access_log.? ? ????? ? # Access Log location???? ? # must have Write permission, currently defined user???? ? # Write process open, write, close, buffer can be set?? ? ? Location /{?? #根据URL进行不同配置? ? ? ? ?????? ? # ~ Case-sensitive???? ? # ~* case insensitive????? #/Match all???? ? # = Exact match, stop searching other location????? # When ^~ matches,Stop searching for other location????? ##################### #location匹配优先级 ########################?????#= Exact match will be processed first. If an exact match is found, Nginx stops searching for other matches. #普通字符匹配, regular expression rules and long block rules will be matched by precedence and query, that is, if the match needs to be seen there are no regular expression matches and longer matches. #^~ only matches this rule, Nginx stops searching for other matches, otherwise Nginx will continue to process other location instructions. ????? #最后匹配理带有 "~" and "~*" instructions, if the corresponding match is found, Nginx stops searching for other matches, when there is no regular expression or no regular expression is matched, then the most matched verbatim matching instructions will be used.

????. proxy_pass. ???!????. proxy_redirect?? ??? #在响应头更新Location和Refresh在代理服务器????? ? Proxy_set_header? Host?????? $host;?? #许重新定义和添加request header on the proxy server? ?????? ? Proxy_set_header? X-real-ip ???? $remote _addr;???? # Proxy_set_header? X-forwarded-for? $proxy _add_x_forwarded_for;?Client_max_body_size???????? 10m;?? #request body Maximum size???? . client_body_buffer_size? 128k;? ??#request Body Buffer Size?Client_body_temp_path????????.. /var/nginx/client_body_temp;?? #缓冲临时写入文件???????. #超时时间连接到后端服务器, generally not more than 75 seconds .???????. #request连到到后端服务器时间, only two transmissions, not beginning to end .????. proxy_read_timeout? ??????? #读取代理服务器响应时间?????. Proxy_send_lowat.??? ?. 12000;?? #FreeBSD才有用?Proxy_buffer_size????? 4k;?? #读取代理服务器, Response buffer???????????. 4 32k;?? #数量和缓冲大小读取answer和proxy Server???? proxy_busy_buffers_size?? 64k;???? . proxy_temp_file_write_size 64k;?? #写入临时文件缓存?Proxy_temp_path?????. /var/nginx/proxy_temp;?? #request写入位置?????. CharSet. koi8-r;?? #指定Content-type Response Encoding? ? ? }???? error_page? 404/404.html;?? #错误页面位置???? location/404.html {?????????????? /spool/www. Document Root Path/spool/www/404.html?????. charset??? ? on;????. Source_charset koi8-r;?? #允许重新编码 Proxy Server or Fastcgi-server? ? ? }???? location/old_stuff/{???? rewrite? ^/old_stuff/(. *) $?/new_stuff/$1? permanent;? ? ? }???? location/download/{???? ?? valid_referers blocked Server_names *.example.com;????? if ($invalid _referer) {????? #rewrite ^/? http://www.example.com/;??????? 403;? ? ? ? }????? # Rewrite_log? on;???? # Rewrite/download/*/mp3/*.any_ext To/download/*/mp3/*.mp3???? rewrite ^/(download/.*)/mp3/(. *) \. *$/$1/mp3/$2.mp3 break;?????. root????/spool/www;???? # AutoIndex?? on;???? ? access_log/var/log/nginx-download.access_log? download;? ? ? }?? ? ? ? ? ################### #Nginx rewrite################## ???? ? # Rules-related commands If,rewrite,set,return,break????? # Break: Complete the current rule, not processing the rewrite command???? ? # if: Conditional judgment, only support single if???? ?? # #条件:????? ?? 1 Variable Name: Does not include an empty string or a 0 start character????? ?? 2 variable can be used = equals! = Not equal to????? ??? 3 regular expressions can use ~* and ~????? ?? 4 ~ Match Case Letters????? ??. 5 ~* Match case-insensitive letters????? ?? 6! ~ and! ~* effect with 5 opposite????? ??? 7-f file Exists????? ?? 8-d Directory exists????? ?? 9-e determine whether a file or directory exists???????? 10-x files can be executedyou can use $1,$2,$3 to refer to the following section of regular expressions () .???? ? # return: End the rule and return the status code to the client? ?????? ? # Rewrite: Redirect URIs or modify strings based on an expression???? ? # # #Flag标记???????? 1 Last means complete rewrite???????. 2 break after this match is complete, the match is terminated???????? 3 redirect return 302, temporary redirect, browser will jump after URL address???????? 4 Permanenet returns 301, permanent redirection, the browser will jump after the URL address???? ? # Set: Used to assign a value to a variable
??? location ~* ^.+\. (jpg|jpeg|gif) $ {????. root????/spool/www;???? ? Access_log off;Expires???????? 30d;?? #控制到期时间? ? ? }? ? }? }

Nginx Learning Summary

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.