Linux10.12 Nginx Agent

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx Proxy diagram

Nginx forward Proxy
It is uncommon for nginx to use the scene with a proxy. Requirement Scenario 1: If in the room, only one machine can be networked, the other machine only intranet, the intranet of the machine want to use the Yum installation package, on the machine can be networked to configure a forward proxy.
Nginx Forward proxy configuration file
server {    listen default_server;    Resolver 119.29.29.29;    Location/    {        Proxy_pass http:/$host $request_uri;    }}

Set as the default host, remember to delete or modify the default host set previously.

Resolver

Syntax: Resolver address;address is the address of the DNS server, the domestic common DNS 119.29.29.29 is provided for Dnspod company. International Universal DNS 8.8.8.8 or 8.8.4.4 is available for Google. Others can refer to the http://dns.lisect.com/    example: Resolver 119.29.29.29;

Default_server

The reason is to set as the default virtual host, because it does not have to set the server_name, any domain name resolution can be accessed normally.

Proxy_pass

This directive is used to set the target URL to be proxied, and the forward proxy setting will be maintained at that fixed value. A detailed explanation of the directive is in the reverse proxy configuration.
Nginx Reverse Proxy
Nginx reverse proxies are used in a variety of production environments. Scenario 1: The domain name does not have the record, may the domain name resolves to Hong Kong one cloud host, in Hong Kong cloud host to make an agent, but the website data is on the mainland server.

Example

server {    listen;    server_name google.com;        Location/    {        proxy_pass http://123.23.13.11/;        Proxy_set_header Host   $host;        Proxy_set_header x-real-ip      $remote _addr;        Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;    }}

If you do not set Proxy_set_header, Proxy_pass sets What, what is the backend server to access.

Proxy_header

[Email protected] ~]# curl-i www.baidu.comHTTP/1.1 okaccept-ranges:bytescache-control:private, No-cache, No-store , Proxy-revalidate, No-transformconnection:keep-alivecontent-length:277content-type:text/htmldate:sun, 2018 Jul 02:13:14 gmtetag: "575e1f60-115" Last-modified:mon, June 02:50:08 gmtpragma:no-cacheserver:bfe/1.0.8.18

Proxy_pass

In the forward proxy, the directive has been used. The format is simple: Proxy_pass  url, where the URL contains: Transport protocol (HTTP//, https://, etc.), host name (domain name or ip:port), URI. Examples are as follows: Proxy_pass http://www.aminglinux.com/;p roxy_pass http://192.168.200.101:8080/uri;proxy_pass unix:/tmp/ Www.sock; There are several things you need to be aware of for proxy_pass configuration. Example 1:location/aming/{    proxy_pass http://192.168.1.10;    ...} Example 2:location/aming/{    proxy_pass http://192.168.1.10/;    ...} Example 3:location/aming/{    proxy_pass http://192.168.1.10/linux/;    ...} Example 4:location/aming/{    proxy_pass http://192.168.1.10/linux;    ...} Assuming that server_name is www.aminglinux.com when requesting http://www.aminglinux.com/aming/a.html, the result of each of the above example 1-4 visits is an example 1:http:// 192.168.1.10/aming/a.html Example 2:http://192.168.1.10/a.html Example 3:http://192.168.1.10/linux/a.html example 4:http:// 192.168.1.10/linuxa.html

Note: In any case, add slashes/, such as examples 2 and 3. Installing this comment does not occur when the Proxy_redirect configuration is in progress.

Proxy_set_header

The Proxy_set_header is used to set the header information received by the proxy server. Syntax: Proxy_set_header field Value;field is the item you want to change, as well as the name of the variable, such as the value of Hostvalue as a variable if you do not set Proxy_set_header, the value of the default host is Proxy_ After pass the domain name or IP (general write IP), such as Proxy_pass in Example 3, the request to the backend server, the full request URI is: http://192.168.1.10/linux/a.html if set proxy_set_ header, such as Proxy_set_header host $host; example 3 in Proxy_pass, the full URI of the server requesting to the backend is: http://www.aminglinux.com/linux/a.htmlproxy _set_header X-real-ip $remote _addr, and Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Used to set the remote client IP received by the proxy, if not set, the header information does not pass through the remote real client IP address.        The following example can be used to test: Example 1 (proxy side) Server{listen 8080;server_name Www.aminglinux.com;root/tmp/123.com_8080;index index.html;    location/linux/{echo "$host";    echo $remote _addr; echo $proxy _add_x_forwarded_for;}}    Example 2 (on a proxy server) server {Listen 80;    server_name www.aminglinux.com;      location/aming/{proxy_pass http://192.168.1.10:8080/linux/;p roxy_set_header host $host;p Roxy_set_header X-real-ip        $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_foRwarded_for; }}

Proxy_redirect (few usage scenarios)

This instruction is used to modify the location header and "Refresh" header fields in the response headers returned by the proxy server.    The syntax structure is: Proxy_redirect redirect Replacement;proxy_redirect default;proxy_redirect off; example 1:server {Listen 80;    server_name www.aminglinux.com;    Index index.html;    Location/{proxy_pass Http://127.0.0.1:8080;proxy_set_header host $host;p roxy_set_header x-real-ip $remote _addr;    Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; }} When the requested link is http://www.aminglinux.com/aming the result will return 301, directed to http://www.aminglinux.com:8080/aming/Note: Return 301 has several prerequisite 1. The location behind must be/; 2. Proxy_pass The URL can not be added to the URI, only the IP or Ip:port end, and can not end; 3. The URI to be accessed must be a real-world directory, such as Aming, where the 4 must be present. When you visit, you can't end with a www.aminglinux.com/aming. Although these 4 conditions are harsh, they do encounter similar requests.    The workaround is to add a line Proxy_redirect http://$host 8080//; example 2:server {Listen 80;    server_name www.aminglinux.com;    Index index.html; Location/{proxy_pass Http://127.0.0.1:8080;proxy_set_header host $host;p roxy_redirect/$host: 8080//;p Roxy_    Set_header X-real-ip $remote _addr; Proxy_set_header x-fOrwarded-for $proxy _add_x_forwarded_for; }}

  

Linux10.12 Nginx Agent

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.