Nginx main application

Five main directions:

Static web server

Load Balancing

Static agents

Static and dynamic separation

Web Hosting

Static web server

Nginx is a HTTP web server, static files (such as HTML, images, etc.) on the server can be returned to the browser client via HTTP protocol;

Case:

Configuring the local static resource services

The ace front-end framework (written bootstrapt) of the Demo project, both static page resources deployed to the server nginx is located

1, upload resources

2, the configuration nginx.conf 

 Get nignx installation directory

 Configuration profiles nignx.conf

The first way

server {
        listen       80;#端口号
        server_name  localhost;

        location /ace {#拦截访问资源为ace的所有请求
            root   /opt;#静态资源路径,服务器上的真实路径
            index  index.html;#不一定具体资源时,默认访问首页
        }
}

              root / opt directory resources   

http://jenkins-1/ace/index.html

 

 The second way

server {
        listen       80;#端口号
        server_name  localhost;

        location / {#拦截访问资源为ace的所有请求
            root   /opt/ace;#静态资源路径,服务器上的真实路径
            index  index.html;#不一定具体资源时,默认访问首页
        }
}

 

Load Balancing

Load balancing refers generally to a request "homogeneous" allocated to multiple server nodes in the cluster execution herein refers to uniform within a relatively large range of statistics is substantially uniform, not entirely uniform;

Hardware Load Balancing

For example F5, SINFOR, Array and the like;

Some manufacturers have the advantage of a professional technical service team to provide support, stable performance;

The disadvantage is that expensive, for smaller-scale network applications cost is too high;

Load balancing software

For example Nginx, LVS, HAProxy the like;

The advantage is free and open source, low cost;

Configure load balancing: case are as follows

 

1, set up and enable the two tomcat

[root@jenkins-1 tomcat]# ll
总用量 8
drwxr-xr-x. 9 root root 4096 12月 12 09:53 apache-tomcat-8.5.47-1
drwxr-xr-x. 9 root root 4096 12月 12 09:55 apache-tomcat-8.5.47-2

检查是否正常启用
[root@jenkins-1 tomcat]# netstat -tln | grep 8081
tcp        0      0 :::8081                     :::*                        LISTEN      
[root@jenkins-1 tomcat]# netstat -tln | grep 8082
tcp        0      0 :::8082                     :::*                        LISTEN  

    

2, the configuration nginx.conf

1、在http模块加上
upstream www.myweb.com {
    server  192.168.168.25:8081 weight=1; 
    server  192.168.168.25:8082 weight=1; 
}

其中weight=1表示权重,用于后端服务器性能不均的情况,访问比率约等于权重之比,权重越大访问机会越多;

upstream是配置nginx与后端服务器负载均衡非常重要的一个模块,并且它还能对后端的服务器的健康状态进行检查,若后端服务器中的一台发生故障,则前端的请求不会转发到该故障的机器;

2、在server模块里添加:
location /myweb {
	proxy_pass http://www.myweb.com;
}
其中 www.myweb.com 字符串要和 upstream 后面的字符串相等;

3, access authentication

 

Nginx popular load balancing strategy

1, the polling (default)

     Each request in turn assigned to a different back-end server, if the back-end server is down, automatically removed;

     upstream backserver { 
                    server 127.0.0.1:8080; 
                    server 127.0.0.1:9090; 
            } 

2, weight

      每个请求按一定比例分发到不同的后端服务器,weight值越大访问的比例越大,用于后端服务器性能不均的情况;

      upstream backserver { 
                    server 192.168.0.14 weight=5; 
                    server 192.168.0.15 weight=2; 
             } 

3、ip_hash(可以解决session丢失问题,实现快速实现session共享;但是如果节点挂掉并且没有做session共享,只能再次登入

      ip_hash也叫IP绑定,每个请求按访问ip的hash值分配,这样每个访问客户端会固定访问一个后端服务器,可以解     决会话Session丢失的问题;

      upstream backserver { 
                    ip_hash; 
                    server 127.0.0.1:8080; 
                    server 127.0.0.1:9090; 
             }

4、最少连接

      web请求会被转发到连接数最少的服务器上;

      upstream backserver { 
                   least_conn;
                   server 127.0.0.1:8080; 
                   server 127.0.0.1:9090; 
              } 

负载均衡其他几个配置

1、备用服务配置

      upstream backserver { 
                  server 127.0.0.1:9100;
                  server 127.0.0.1:9200 backup; (其它所有的非backup机器down的时候,才请求backup机器) 
             }

2、设置某服务为宕机状态

      upstream backserver { 
                    server 127.0.0.1:9100;
                    server 127.0.0.1:9200 down; (down表示当前的server是down状态,不参与负载均衡) 
              } 

 

 

静态代理

注意:在一些大厂商中,一般不会这么使用,因为涉及到一些下载或者用户访问量极大时,nginx的处理能力虽然能够达到负载和削峰的要求,但是nginx单节点的带宽时有限的,此时QPS还是提升不上去,此时就需要用到keepalive,后端tomcat处理完请求之后不返回给nginx由其依次逐级响应给客户端,而是由后端tomcat服务器直接相应给用户端

把所有静态资源的访问改为访问nginx,而不是访问tomcat,因为nginx更擅长于静态资源的处理,性能更好,效率更高;

所以在实际应用中,我们将静态资源比如图片、css、html、js等交给nginx处理,而不是由tomcat处理;

Nginx静态代理如何实现?(通过在Nginx的nginx.conf文件进行配置即可实现)

方式一:

1、通过在nginx.conf配置文件中添加静态资源的location

      #当访问静态资源,则从linux服务器/opt/static目录下获取

                     带资源后缀匹配:/opt/static/myweb.jpg
             location ~ .*\.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
                   root /opt/static;
             }

       ~ 表示正则匹配,也就是说后面的内容可以是正则表达式匹配;

       第一个点 . 表示任意字符;

       * 表示一个或多个字符;

       \. 是转移字符,是后面这个点的转移字符;

        | 表示或者

        $ 表示结尾

        整个配置表示以 .后面括号里面的这些后缀结尾的文件都由nginx处理;

        放置静态资源的目录,要注意一下目录权限问题,如果权限不足,给目录赋予权限;

方式二

1、我们将静态资源放入 /opt/static 目录下,然后用户访问时由nginx返回这些静态资源;

     http://localhost/myweb/image/001.jpg

     资源路径匹配:/opt/static/myweb/image/

     location ~ .*/(css|js|img|images|image) {
                   root   /opt/static;
            }

 

动静分离

描述

Nginx的负载均衡 和 静态代理 结合在一起,我们可以实现动静分离,这是实际应用中常见的一种场景;

动态资源,如jsp由tomcat或其他web服务器完成;

静态资源,如图片、css、js等由nginx服务器完成;

它们各司其职,专注于做自己擅长的事情;

动静分离充分利用了它们各自的优势,从而达到更高效合理的架构;

A:nginx:做负载均衡

B:nginx:做静态代理

C:nginx:做静态代理

动静分离示例

1、负载均衡Nginx配置(A节点配置

#http中配置上游
#两个tomcat服务
upstream www.p2p.com { 
    server  127.0.0.1:9100 weight=5; 
    server  127.0.0.1:9200 weight=2;  
}
#两个nginx提供的静态代理资源    
upstream static.p2p.com { 
    server  127.0.0.1:81 weight=1; 
    server  127.0.0.1:82 weight=1;  
}


#server中配置
#代理到tomcat服务
location /p2p {
    proxy_pass http://www.p2p.com;
}
#代理到nginx提供的静态代理资源服务
location ~ .*/(css|js|img|images) {
    proxy_pass http://static.p2p.com;
}

2、静态代理Nginx配置(B\C节点配置

#nginx静态代理配置
location ~ .*/(css|js|img|images) {
    root /opt/static;
}

 

虚拟主机

描述

虚拟主机,就是把一台物理服务器划分成多个“虚拟”的服务器,这样我们的一台物理服务器就可以当做多个服务器来使用,从而可以配置多个网站;

Nginx提供虚拟主机的功能,就是为了让我们不需要安装多个Nginx,就可以运行多个网站;

Nginx下,一个server标签就是一个虚拟主机;

nginx的虚拟主机就是通过nginx.conf中server节点指定的,想要设置多个虚拟主机,配置多个server节点即可;

配置虚拟主机通过有下面两种方式:

1、基于域名的虚拟主机

      基于域名的虚拟主机是最常见的一种虚拟主机。

server {
        listen       80;
        server_name  www.myweb.com;
        location /myweb {
           proxy_pass http://www.myweb.com;
        }
}
server {
        listen       80;
        server_name  www.p2p.com;
        location /p2p {
           proxy_pass http://www.p2p.com;
        }
}

     需要修改一下本地的hosts文件,文件位置:C:\Windows\System32\drivers\etc\hosts

     在hosts文件配置:192.168.208.128 www.myweb.com

     在hosts文件配置:192.168.208.128 www.p2p.com

     前面是Linux的IP,后面是你自定义的域名

2、基于端口的虚拟主机

      基于端口的虚拟主机配置,使用端口来区分;

       浏览器使用 同一个域名+端口 或 同一个ip地址+端口访问;

server {
    listen 8080;
    server_name www.myweb.com;
    location /myweb {
           proxy_pass http://www.myweb.com;
    }
}
server {
    listen 9090;
    server_name www.myweb.com;
    location /p2p {
           proxy_pass http://www.p2p.com;
    }
}

 

虚拟主机案例:

城市站点网站(举例说明,我们配置三个城市站点)

1、配置三个tomcat服务 

部署三个tomcat,工程war放到ROOT下并解压,分别为beijing.war/nanjing.war/tianjing.war
对应tomcat分别为tomcat1:8081,tomcat2:8082,tomcat3:8083
[root@jenkins-1 tomcat]# ll
总用量 12
drwxr-xr-x. 9 root root 4096 12月 12 09:53 apache-tomcat-8.5.47-1
drwxr-xr-x. 9 root root 4096 12月 12 09:55 apache-tomcat-8.5.47-2
drwxr-xr-x. 9 root root 4096 12月 12 15:36 apache-tomcat-8.5.47-3

 2、在nginx.conf文件添加三个server节点,用于配置三个虚拟主机

    方式一:

    server {
        listen       80;
        server_name  beijing.myweb.com;

        location / {
            proxy_pass http://beijing.myweb.com;
        }
    }

    server {
        listen       80;
        server_name  nanjing.myweb.com;

        location / {
            proxy_pass http://nanjing.myweb.com;
        }
    }
    server {
        listen       80;
        server_name  tianjin.myweb.com;

        location / {
            proxy_pass http://tianjin.myweb.com;
        }
    }

     方式二:

      通过include的方式引入虚拟主机配置

      include /usr/local/nginx/vhost/vhost.conf;    文件内容同上三个虚拟主机配置

      将虚拟目录的配置文件加入到”http {}”部分的末尾,与其他server并列;

 3、配置每个虚拟主机请求转发所对应的后端服务器

    upstream beijing.myweb.com {
        server  192.168.164.25:8081;
    }

    upstream nanjing.myweb.com {
        server  192.168.164.25:8082;
    }

    upstream tianjin.myweb.com {
        server  192.168.164.25:8083;
    }

 4、修改hosts文件,让Linux的ip指向到一个三个站点的域名

       192.168.164.25 beijing.myweb.com
              192.168.164.25 nanjing.myweb.com
              192.168.164.25 tianjin.myweb.com

 5、效果展示

 

 

 

 

 

发布了92 篇原创文章 · 获赞 3 · 访问量 5134

Guess you like

Origin blog.csdn.net/qq_22049773/article/details/103491247