Nginx Load Balancer Server Getting Started case

Source: Internet
Author: User
Tags sendfile web hosting nginx reverse proxy
have a short time recently, study a bitNginx , and made a simple introductory case.

Brief introduction:

Nginx (engine x) is a lightweight Web Server, reverse proxy server, and e-mail (imap/pop3) proxy server.

The Normal mode of client Access server is direct access, with nginx Server, we can deploy the same application to different servers, the access mode is as follows: This greatly improves the concurrency capability, reduces the pressure on the server and improves the performance.

Normal access mode:



Nginx access mode:


Advantage:

as a Web server: Compared to Apache,nginx uses less resources, supports more concurrent connections, and embodies higher efficiency, which makes nginx is especially popular with web hosting providers. Capable of supporting responses up to 50,000 concurrent connections, thanks to Nginx for choosing epoll and Kqueue As a development model .

as a Load balancer server: Nginx can support both Rails and PHPinternally, as well as HTTP proxy servers for external service. Nginx is written in C , Whether it is system resource overhead or CPU The efficiency of use is much better than Perlbal .

as a mail proxy : Nginx is also a very good mail proxy server (one of the first to develop this product is also as a mail proxy server),last.fm Describes the success and the wonderful use of experience.

Nginx installation is very simple, the configuration file is very concise (also can support perl syntax),Bugs very few servers : Nginx start-up is particularly easy and can be done almost 7x24 runs continuously, and does not require a restart even if it runs for several months. You will also be able to upgrade your software version in the event of uninterrupted service.

Installation:

have a Linux and Windows versions, the foundation itself needs to be installed accordingly. I take windows for example:

Download the installation package http://nginx.org/en/download.html

Unzip, click nginx.exe Run to start the service, enter localhost in the browser (the default listener 127.0.0.1:80 Enter the following page to indicate that the nginx Server started successfully.


Demo:

I built my own springboot simple project, deployed under different ports (can be deployed to different servers). To facilitate testing, I made a simple distinction between the contents of the output:


Detailed configuration:

Note:conf/nginx.conf is The default configuration file for Nginx. You can also use nginx-c to Specify your configuration file

#运行用户 #user Nobody; #启动进程, usually set to equal to the number of CPUs worker_processes 1; #全局错误日志 #error_log logs/error.log; #error_log logs/    Error.log notice; #error_log logs/error.log info; #PID文件, records the currently activated Nginx process Id#pid logs/nginx.pid; #工作模式及连接数上限events { Worker_connections 1024;}    #设定http服务器, using its reverse proxy feature to provide load balancing support for HTTP {#设定mime类型 (mail support type), the type is defined by the Mime.types file include mime.types;    Default_type Application/octet-stream; #设定日志 #log_format main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _b    Ytes_sent "$http _referer" ' # ' "$http _user_agent" $http _x_forwarded_for ";    #access_log Logs/access.log Main; #sendfile instruction Specifies whether Nginx calls the Sendfile function (zero copy mode) to output files, for normal applications, #必须设为 on, if used for downloading applications such as disk IO heavy-duty applications, can be set to off to balance disk and network I/O processing speed    and reduce the uptime of the system.    Sendfile on;    #tcp_nopush on;    #连接超时时间 Keepalive_timeout 65;    #gzip压缩开关 #gzip on; #设定负载均衡的服务器列表 (or a reverse proxy server, one that does not need to be assigned a weight) upstream Hello_server {#weigthParameters represent weights, the higher the weight, the greater the probability of being allocated to the server 127.0.0.1:8888 weight=1;    Server 127.0.0.1:8899 weight=3;        } server {#监听80端口, port 80 is a well-known port number for HTTP protocol listen; #定义使用localhost访问 server_name localhost;        #编码格式charset Utf-8;            #access_log Logs/host.access.log main; #反向代理的路径, set map path after location # Load balance requests for all requests (and upstream bindings) location/{                 Root/root;  #定义服务器的默认网站根目录位置 index index.html index.htm;   #定义首页索引文件的名称 Proxy_pass Http://hello_server;            #请求转向hello_server defined list of servers #以下是一些反向代理的配置 (optionally configurable) #proxy_redirect off;            Proxy_set_header Host $host;            Proxy_set_header X-real-ip $remote _addr;            #后端的Web服务器可以通过X-forwarded-for Get the user real IP proxy_set_header x-forwarded-for $remote _addr;          Proxy_connect_timeout 90;             #nginx跟后端服务器连接超时时间 (proxy connection timeout) Proxy_send_timeout 90; #后端服务器数据回传时间 (proxy send timeout) proxy_read_timeout 90;            #连接成功后, back-end server response time (proxy receive timeout) Proxy_buffer_size 4k;               #设置代理服务器 (Nginx) Save the user header information buffer size Proxy_buffers 4 32k;       #proxy_buffers缓冲区, the average page below 32k, so set proxy_busy_buffers_size 64k;    #高负荷下缓冲大小 (proxy_buffers*2) proxy_temp_file_write_size 64k;          #设定缓存文件夹大小, greater than this value, will be transmitted from the upstream server client_max_body_size 10m;      #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数}}

once configured , restart the Nginx server to access localhost:

Restart command : nginx-s Reload

an nginx (localhost) server is accessed , but is actually accessing the deployed springboot project (localhost:8899) . A few more refreshes can be seen, stating that the services deployed on both ports are accessed.


Note: The weight values for the two service assignments are different, so Hello World b appears!!! ( actual access to localhost:8899) is more likely to appear Hello World a!!! ( actual access to localhost:8888) is less likely. You can modify the permission value yourself, and then test it a few more times!

Summarize:

Nginx Common commands:



Related recommendations:

Use Nginx to avoid cross-domain requests for Ajax

Nginx Reverse proxy mechanism solves the problem of front-end cross-domain

Related Article

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.