Detailed Nginx reverse proxy and load Balancing Deployment Guide _nginx

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx reverse proxy and load Balancing Deployment guide, as follows:

1. Installation

1 from the Nginx official website download page (http://nginx.org/en/download.html) download Nginx The latest version (is currently 1.5.13 version) installation package;

2 after decompression, copy to the deployment directory.

2. Start and stop Nginx

Nginx currently only supports command-line operations, before operation into the DOS command environment, and into the Nginx deployment directory.

1) Start Nginx:start nginx

2 Stops Nginx:nginx-s stop

3) After modifying the configuration restart: nginx-s Reload

These three commands can be made into a bat file, placed in the deployment directory, to facilitate subsequent operations.

Start Nginx.bat file content: Start Nginx

Stop Nginx.bat file contents: Nginx-s stop

Reload Nginx.bat file content: Nginx-s Reload

3. Reverse proxy configuration

Modify the contents of the nginx.conf file (such as nginx-1.5.13\conf\nginx.conf) of the Conf subdirectory under the deployment directory to adjust the related configuration.

Reverse Proxy Configuration Example:

Location/{

    #设置主机头和客户端真实地址 so that the server obtains the client real IP

       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_buffering off;

       #设置反向代理的地址

       proxy_pass http://192.168.1.1;   

   }

The proxy address is modified according to the actual situation.

4. Load Balanced Configuration

Nginx's upstream default is to achieve load balancing by polling, in which each request is assigned to a different back-end server in chronological order, and can be automatically removed if the backend server is down.

Another way is to Ip_hash: Each request is allocated according to the hash result of the access IP, so that each visitor has a fixed access to a back-end server that resolves the session's problem.

Example Load Balancing configuration:

Upstream backend {

       #ip_hash;

       Server 192.168.1.251;

       Server 192.168.1.252;

       Server 192.168.1.247;

     }

server {

    listen    ;

    server_name trffweb; 

    Location/{

       #反向代理的地址

       proxy_pass http://backend  

    }

}

Upstream naming and server addresses are modified according to the actual situation.

5. Complete Configuration Example

Nginx.conf:

Worker_processes 1;

Events {

  worker_connections 1024;

}

HTTP {

  include    mime.types;

  Default_type Application/octet-stream;

  Sendfile on    ;

  Keepalive_timeout;

Upstream backend {

       #ip_hash;

       Server 192.168.1.251;

       Server 192.168.1.252;

       Server 192.168.1.247;

     }

  server {

    listen    ;

    server_name 2; 

    Location/{

    #设置主机头和客户端真实地址 so that the server obtains the client real IP

       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_buffering off;

       #反向代理的地址

       proxy_pass http://backend  

    }}

  }


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.