Nginx installation and deployment and load balancing settings

 

Nginx is a free, open source, high-performance and lightweight HTTP server and reverse proxy server, its performance is comparable to IMAP/POP3 proxy server. Nginx is known for its high performance, stability, rich functions, simple configuration and less system resources.

Nginx surpasses Apache's high performance and stability, making more and more websites use Nginx as a web server in China

 

The official website of Nginx : http://nginx.org

Nginx document address: http://nginx.org/en/docs/

 

Next, we use Nginx to explain its installation, deployment and debugging.

 

Nginx download address: http://nginx.org/download/nginx-1.8.1.zip

 

The installation of Nginx is very simple. You can use it after decompressing it locally. Enter " nginx -v " in the installation root directory to see the version information of the currently installed nginx .

 

After Nginx is installed, the directory structure is shown in the following table:

├─conf

├─contrib

├─unicode2nginx 

└─vim 

├─ftdetect     

├─indent     

└─syntax     

├─docs

├─html

├─logs

└─temp

    ├─client_body_temp

    ├─fastcgi_temp

    ├─proxy_temp

    ├─scgi_temp

    └─uwsgi_temp

illustrate:

1. The conf folder stores some configuration files of Nginx , among which the "nginx.conf" file is frequently used in the configuration process.

2. Contrib folder _ _

3. docs store protocol and descriptive stuff

4. The html folder stores the page files that Nginx provides access by default

5. The logs folder, the two most important logs here are access.log and error.log . The former is mainly used to check which resources of the cluster are provided to the outside world after the server is accessed daily; the latter is mainly used to check the Nginx server . Whether it is running normally, if the configuration of the nginx .conf file is incorrect, it will be printed here. At the same time, if you use Nginx as the cluster soft load, if a server in the load balancing configuration suddenly stops working, it can be detected here in time.

6. temp is a temporary folder

 

There are three ways to start Nginx under Windows :

1. Click nginx.exe directly to start the service

2. In the root path of the nginx installation, the command line can directly enter nginx.exe and press Enter to start the service, but the service started in this way will keep the command line in a parked state and cannot perform other operations, so use it not convenient.

3. Under the nginx root path, execute the command " start nginx.exe " on the command line. At this time, you will find that a temporary window flashes suddenly after execution. After opening the webpage, try to see if localhost can be displayed normally. The default welcome interface of nginx indicates that the startup is successful.

 

There are two ways to stop nginx under Windows :

1. Execute the command: n ginx .exe -s quit

2. Execute the command: nginx.exe -s stop

The difference between them is: stop is to quickly stop nginx and may not save relevant information. Quit stops nginx in a complete and orderly manner and saves relevant information. This is somewhat similar to the difference between a forced shutdown and a soft shutdown of a computer.

 

When reloading Nginx under Windows , you can execute the " nginx.exe -s reload " command.

 

Nginx has four commonly used functions: static server, virtual host, reverse proxy, load balancing

 

Nginx对各种需求的配置均非常简单,加入你想使用Nginx的负载均衡能力,可以在nginx.conf文件中的http节点内配置upstream属性,指明负载均衡的名称,并通过在server节点中设置反向代理指向你命名的负载均衡名称,则最基本的负载均衡配置成功。

 

我们以一台主机中同时配置多个服务,并通过Nginx提供负载均衡能力为例说明:

 

假设本次只有两个应用服务端做集群,以tomcat为例,配置时,一定要注意将两个tomcat里只要有端口的地方都需要调整,否则另外一个tomcat将无法正常工作,调整完成后,启动两台已经配置好了的tomcat。通过网页指定端口进行访问,确认下服务是否已经正常启动了。

 

在确保正常启动以后,为了配置集群后能够区分出当前访问的是哪台主机,你可以在tomcatROOT路径下修改欢迎界面index.jsp,在页面的显著位置增加一个标识来标注服务器来源。这样你在后面配置了负载均衡以后,就可以明显看到你访问的具体是哪一台机器。

调整后的nginx.conf配置如下:

    upstream tomcat_schedule_web_test {

        server 192.168.56.1:8080;

        server 192.168.56.1:8099;

    }

    server {

        listen       80;

        #server_name  localhost;

        #指明index

        index index.jsp index.html index.htm;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            #root   html;

            #index  index.html index.htm;

            #反向代理指明访问的流路径

            proxy_pass http://tomcat_schedule_web_test;

        }

说明:

1、配置中没有做改动的地方本次没有贴出来

2、增加了upstream属性配置,指向了同一台机器的不同端口提供的tomcat服务。

3、在location属性中增加了对upstream的反向代理支持

4、外部访问不需要知道两台tomcat服务的端口号,直接输入localhost就可以访问。

5、本配置是nginx提供软负载功能的基本配置,实际应用中需要考虑集群中单台服务的能力差别,根据实际情况合理分配和支出单台应用的性能,所以配置要比目前看到的负责一些,这里说明的只是最基本的配置情况。

 

 

 访问下Nginx,是不是生效了呢 ^_^

 

 

 

各位看官,原创不易啊,转载请注明出处: http://danlley.iteye.com 看在打字不易的份上,打赏一个吧

 

参考资料:

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326424411&siteId=291194637