Nginx Reverse proxy configuration and optimization-nginx optimization

Source: Internet
Author: User
Tags documentation servervariables nginx reverse proxy

The environment is Windows + nginx. (the latest version is 1.5.10, which is recommended in the official documentation ).

1. Simple configuration to enable nginx to run first

Nginx configuration is relatively simple. Find the nginx. conf file in the conf directory and modify the following configuration:

The code is as follows: Copy code

Server {
Listen 80; // listening port bound
Server_name www.111cn.net 111cn.net; // The bound domain name.
 
# Charset KOI8-R;
 
# Access_log logs/host. access. log main;
 
Location /{
Root html;
Proxy_pass http: // 192.168.0.23: 81/; // The http server on the intranet
Proxy_redirect off;
Proxy_set_header Host $ proxy_host; // Host name and port of the backend server
Proxy_set_header X-Real-IP $ remote_addr; // reverse proxy IP, where X-Real-IP is the custom request header
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Index. aspx default. aspx index.html index.htm; // default document
        }

Note: proxy_set_header Host $ proxy_host; in this configuration, the official documentation is proxy_set_header Host $ host. However, after testing, reverse proxy cannot be enabled successfully, and no embedded variables in this document are $ host, I do not know whether the document is wrong or my understanding is wrong.
Switch cmd to the nginx Directory, run start nginx, and run nginx. there are two more nginx.exe processes in the process. run nginx-s stop to disable nginx and run nginx-s reload to restart nginx. note: if you disable or restart the command, you cannot disable nginx. If the configuration modification is invalid, check whether nginx actually exits.

2. Request header changes after reverse proxy

Let's take a look at what is added to the request header after reverse proxy is enabled. The code for retrieving the request header in the last php section

 

The code is as follows: Copy code
<? Php
$ Headers = array ();
Foreach ($ _ SERVER as $ key => $ value ){
Echo $ key. "=". $ value;
Echo "<br/> ";
If ('http _ '= substr ($ key, 0, 5 )){
$ Headers [str_replace ('_', '-', substr ($ key, 5)] = $ value;
    } 
}
?>

After accessing this script through reverse proxy, the following results are obtained:

The code is as follows: Copy code
USER = www
HOME =/home/www
FCGI_ROLE = RESPONDER
GATEWAY_INTERFACE = CGI/1.1.
SERVER_SOFTWARE = nginx/1.2.9
QUERY_STRING =
REQUEST_METHOD = GET
CONTENT_TYPE =
CONTENT_LENGTH =
SCRIPT_NAME =/tools/test. php
REQUEST_URI =/tools/test. php
DOCUMENT_URI =/tools/test. php
DOCUMENT_ROOT =/web
SERVER_PROTOCOL = HTTP/1.1
REMOTE_ADDR = 123.170.241.72
REMOTE_PORT = 11827
SERVER_ADDR = 103.251.91.175
SERVER_PORT = 80
SERVER_NAME = www.111cn.net
REDIRECT_STATUS = 200
SCRIPT_FILENAME =/web/tools/test. php
HTTP_HOST = 111cn.net
HTTP_CONNECTION = keep-alive
HTTP_CACHE_CONTROL = max-age = 0
HTTP_ACCEPT = text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, */*; q = 0.8
HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
HTTP_ACCEPT_ENCODING = gzip, deflate, sdch
HTTP_ACCEPT_LANGUAGE = zh-CN, zh, q = 0.8
HTTP_COOKIE = wp-settings-1 = imgsize % 3 Dfull % 26 editor % 3 Dtinymce % 26 hidetb % 3D1% login % 3D1% 26 libraryContent % 3 Dbrowse % 26 align % 3 Dnone % 26 urlbutton % 3 dpost % 26 mfold % 3Do; wp-settings-time-1 = 1386907183; bd1__firstime = 1393228286385; wordpress_test_cookie = WP + Cookie + check; response = NINE % 7C1393388079% 7Cce226d537feb3af00ea52d0cf8d71600
PHP_SELF =/tools/test. php
REQUEST_TIME = 1393261963.

3. Modify the code of the backend website

If the back-end website needs to obtain the visitor's IP address, the "REMOTE_ADDR" attribute in the request gets the ip address of the reverse proxy, in this case, the visitor's real IP address is obtained through the request header added when the reverse proxy forwards the request. currently, my backend website is. net.

The code is as follows: Copy code

Public string getRequstAddr (HttpContext context ){
String ip;
If (context. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]! = Null)
    {
String str = context. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"];
String [] iparray = str. Split (',');
Ip = iparray [0];
    }
Else {
Ip = context. Request. ServerVariables ["REMOTE_ADDR"]. ToString ();
    }
Return ip;
}

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.