Nginx Configuration Learning Summary

Source: Internet
Author: User

1.nginx Reverse Proxy

Before you tell the specific configuration, the difference between the forward proxy and the reverse proxy is the first.

Forward Proxy: is a server located between the client and the original server (Origin server), in order to obtain content from the original server, the client sends a request to the agent and specifies the target (the original server), then the agent forwards the request to the original server and returns the obtained content to the client. The client can use the forward proxy, and some special settings must be made to use the forward proxy. A forward proxy is a proxy method that the end user knows and actively uses.

A typical use of a forward proxy is to provide access to the Internet for LAN clients within the firewall. Forward proxies can also use buffering features to reduce network usage. A forward proxy allows a client to access any Web site through it and hides the client itself, so you must take security measures to ensure that only authorized clients are serviced.

Reverse proxy: Refers to the proxy server to accept the connection request on the Internet, and then forward the request to the server on the internal network, and the results obtained from the server to the Internet to request connections on the client, when the proxy server is displayed as a reverse proxy server. The reverse proxy is transparent to the user and is not perceived by the user.

A typical use of a reverse proxy is to provide the server behind the firewall to Internet users for access. The reverse proxy can also provide load balancing for multiple servers in the backend, or provide buffering services for servers with slower back-end.

Modifying a configuration file/usr/local/nginx/conf/nginx.conf
User www www;worker_processes1; Error_log logs/error.log;pid Logs/nginx.pid;worker_rlimit_nofile 65535; events {use epoll; Worker_connections65535;}    HTTP {include mime.types; Default_type Application/octet-stream; include/usr/local/nginx/conf/vhosts/proxy.conf;    Sendfile on; Keepalive_timeout65;    gzip on; Client_max_body_size 50m;    #缓冲区代理缓冲用户端请求的最大字节数, can be understood as saved to the local and then passed to the user client_body_buffer_size 256k;    Client_header_timeout 3m;    Client_body_timeout 3m;    Send_timeout 3m; Proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间 (proxy connection timeout) Proxy_read_timeout 300s;    #连接成功后, back-end server response time (proxy receive timeout) Proxy_send_timeout 300s; Proxy_buffer_size 64k; #设置代理服务器 (nginx) buffer size to hold user header information Proxy_buffers432k, #proxy_buffers缓冲区, the average web page below 32k, so set proxy_busy_buffers_size 64k; #高负荷下缓冲大小 (Proxy_buffers* *) proxy_temp_file_write_size 64k; #设定缓存文件夹大小, greater than this value, the request will be passed from the upstream server without buffering to disk proxy_ignore_client_abort on; #不允许代 Active shutdown of connection server {Listen80;        server_name localhost; Location/{root HTML;        Index index.html index.htm; } error_page502 503 504/50x.html; Location =/50x.html {root html; }    }}
Edit reverse proxy Server configuration file:/usr/local/nginx/conf/vhosts/proxy.conf
server{Listen80;    server_name jerishi1.com; 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://192.168.10.38:3000;} access_log Logs/jerishi1.com_access.log;}server{Listen80;    server_name jerishi2.com; 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://192.168.10.40:80;} access_log Logs/jerishi2.tk_access.log;}
Load Balancing Settings
upstream Monitor_server {    192.168.0.1:80;     192.168.0.2:80;} server{    ;    server_name nagios.xxx123.tk;     / {        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://monitor_server;     }    access_log logs/nagios.jerishi.com_access.log;}
2.ngnix Restart Check Configuration
$:/usr/local/nginx-1.5.1/sbin/nginx-t
Re-start Nginx
$:/usr/local/nginx-1.5.1/sbin/nginx-s Reload
Close Nginx
$:/usr/local/nginx-1.5.1/sbin/nginx-s stop
3.Location Processing logical Location Matching command
    • ~ #波浪线表示执行一个正则匹配, Case sensitive
    • ~* #表示执行一个正则匹配, Case insensitive
    • ^~ #^~ represents normal character matching, and if this option matches, matches only that option, and does not match the other options, typically used to match the directory
    • = #进行普通字符精确匹配
    • @ # "@" defines a named location, using internal orientation, such as Error_page, Try_files

code example:

Location  =/ {  # matches only '/'.  [Configuration A]} Location  / {  # matches any request because all requests start with "/"   # but longer character matches or regular expression matches will match   ^~/images/ {  /images/ starts the request and stops  matching otherlocation ~*. ( Gif|jpg| jpeg) $ {  # matches the request ending with GIF, JPG, or JPEG.    the request for the/images/directory will be handled by [Configuration C].     [Configuration D]}
Matching process:

1. Test all prefix string with a URI;
2. Uri exact match to = defined loacation, use this location, stop searching;
3. Match the longest prefix string, if the longest prefix string with the ^~ modifier, use this location to stop the search, otherwise:
4. Store this longest match;
5. Then match the regular expression;
6. Match to the first regular expression, use this location to stop the search;
7. There is no match to the regular expression, using the location of the prefix string stored in # # step.

4.nginx Root&alias Comparison Root

Syntax: Root path
Default value: Root HTML
Configuration segment: HTTP, server, location, if
Root is mapped according to the full URI request, which is/path/uri.
Example:

Location ~ ^/qcloud/ {    /data/release/www.qcloud.com;     AutoIndex on;    Auth_basic            "Restricted";    Auth_basic_user_file  passwd/weblogs;}

If the URI of a request is/qcloud/www.buy.qcloud.com/main.js, the Web server will return/data/release/www.qcloud.com/qcloud/ The Www.buy.qcloud.com/main.js file.

Alias

Syntax: Alias path
Configuration segment: Location
Alias discards the path that is configured behind the location, pointing to the directory that is currently matched to the specified directory.
Example:

Location ~ ^/qcloud/ {    /data/release/www.qcloud.com;     AutoIndex on;    Auth_basic            "Restricted";    Auth_basic_user_file  passwd/weblogs;}

If the URI of a request is/qcloud/www.buy.qcloud.com/user.js, the Web server will return the/data/release/www.qcloud.com/www.buy.qcloud.com/main.js file.

Nginx Configuration 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.