关于nginx:nginx简介

6次阅读

共计 7962 个字符,预计需要花费 20 分钟才能阅读完成。

我的 github 该文档地址

nginx 简介

官网:https://nginx.org/

[TOC]

nginx 装置

yum install nginx

nginx 常用命令

名称 命令 备注
查看版本 nginx -v
查找装置门路 find / -name nginx 配置文件个别在 /etc/nginx/nginx.conf
启动 systemctl start nginx
查看状态 systemctl status nginx
从新加载配置文件 nginx -s reload 批改配置文件后,运行它,立即失效
疾速敞开 nginx -s stop
优雅的敞开 nginx -s quit

nginx 动态内容服务

web 服务一个重要工作就是提供文件服务,图片,html 页面

咱们举个例子:

​ 咱们创立一个动态页面,外面有一张图片,这须要咱们在配置文件中设置 http 中 server 中的 location

  • 我的项目文件构造

    [root@VM-0-5-centos data]# pwd
    /data
    [root@VM-0-5-centos data]# tree
    .
    └── fptxl
        ├── fptxl.html
        └── images
            └── rich_woman.jpg
  • 动态文件代码

    动态内容代码

  • nginx 配置

    nginx.conf 文件的 http–>server->location 配置

        # 前缀 "/" 会匹配所有申请,如果有多个 location 匹配一个申请,则抉择前缀最长的
        location / {
            #每个申请加上 root 的地址,就是文件在服务器的实在地址
            root /data/fptxl;
            #默认关上网页
            index fptxl.html
        }
        

    每次更改配置文件后,记得从新加载配置文件

    nginx -s reload

nginx 简略代理服务(前后端拆散例子)

nginx 一个罕用性能是当作代理服务器,承受申请,传给被代理服务器,获取响应,返回给客户端。

+------------+                +------------+                +----------------+
|            | +------------> |            | +------------> |                |
| http client|                |proxy server|                | proxied server |
|            | <------------+ |            | <------------+ |                |
+------------+                +------------+                +----------------+
   browser                        nginx                          tomcat

咱们举个例子:配置一个简略的代理服务用于解决后端申请,nginx 动态内容服务用于解决前端申请,这能够作为一种前后端拆散配置计划。

  1. 代理服务 nginx 配置

    nginx.conf 文件的 http–>server->location 配置

        location /fptxl/ {
            #每个申请都会加上 root 对应的地址,就是文件在服务器的实在地址
            proxy_pass http://localhost:8080/;
        }
    

    proxy_pass: 一个申请如果匹配了 location,匹配 location 的局部会被替换成 proxy_pass, 比方一个公网申请 http://fptxl.com/fptxl/actuat…,”/fptxl/actuator/env” 中匹配前缀 ”/fptxl/” 的局部会被替换为 http://localhost:8080/,最终地址为:“http://localhost:8080/”+“actuator/env”

    通过含有 proxy_pass 的 location,能够将多个不同 ip:port 的服务映射成同一个 ip:port 下不同门路的服务

    占用 8080 端口的是咱们在服务器运行的一个后端服务,用于查看后端服务

  2. 动态内容配置

    本例子中能够间接应用上方 nginx 动态内容服务

    动态文件地位:

    [root@VM-0-5-centos data]# tree
    .
    ├── fptxl
    │   ├── fetch.html
    │   ├── fptxl.html

    fetch.html 内容 拜访地址

    <button>get instance port</button>
    <script>
        const btn = document.querySelector('button');
        btn.addEventListener('click', () => {
            //https 拜访的时候此处会报错,该当应用
            //var url=window.location.protocol+"//fptxl.com/fptxl/actuator/env";
            var promise=fetch('http://fptxl.com/fptxl/actuator/env').then(response => response.json())
                .then(data => {var port=data["propertySources"][0]["properties"]["local.server.port"]["value"];
                    let pElem = document.createElement('p');
                    pElem.textContent = '外部服务端口为:'+port;
                    document.body.appendChild(pElem);
                })
        });
    </script>

    nginx 残缺配置

            # 前缀 "/" 会匹配所有申请,如果有多个 location 匹配一个申请,则抉择前缀最长的
            location / {
                #每个申请都会加上 root 对应的地址,就是文件在服务器的真是地址
                root /data/fptxl;
                #默认关上网页
                index fptxl.html;
            }
            
            # 如果有多个 location 匹配一个申请,则抉择前缀最长的
            location /fptxl/ {
                #每个申请都会加上 root 对应的地址,就是文件在服务器的真是地址
                proxy_pass http://localhost:8080/;
            }

负载平衡

简介

负载平衡通过多个利用实例来实现优化资源利用,最大化吞吐量,进步容错。

nginx 能够提供无效的 HTTP 负载平衡来散发信息到多个 web 利用的应用服务器中来进步性能、吞吐量、可靠性。

负载平衡的机制

nginx 提供以下几种负载平衡机制

轮询:以轮询的形式顺次将申请调度不同的服务器,即每次调度执行 i = (i + 1) mod n,并选出第 i 台服务器。

起码连贯:下一个申请会被指派给连接数起码的服务器

ip 哈希:应用基于客户端 ip 地址的哈希函数来决定应用哪台服务器提供服务

默认的负载平衡配置(轮询)

最简略的负载平衡配置:

http {
    #名称尽量不要应用特殊字符,防止应用了 nginx 关键字
    upstream backend {  #服务器集群名字   
        server    localhost:8080;# 服务器配置    
        server    localhost:18080;# 服务器配置  
    }

    server {
        listen 80;

        location /fptxl/ {proxy_pass http://backend/;}
    }
}

负载平衡默认应用 轮询,nginx 反向代理能够给 HTTP, HTTPS, FastCGI, uwsgi, SCGI, memcached, and gRPC 提供负载平衡。

最小连接数负载平衡

当有些工作耗时较长时,比拟适宜用起码连接数的形式。

黑话:当有些人工作过多时,给他少派发一些工作。

http {
    #名称尽量不要应用特殊字符,防止应用了 nginx 关键字
    upstream backend {  #服务器集群名字  
        least_conn;
        server    localhost:8080;# 服务器配置    
        server    localhost:18080;# 服务器配置  
    }
}

会话长久化(ip-hash)

轮询和最小连接数会使得后续的申请被派发给不同的服务,ip-hash 应用客户的 ip 地址作为哈希的 key 值来决定那个服务器提供服务,如果服务器没有做会话共享,能够应用这种形式。

# 名称尽量不要应用特殊字符,防止应用了 nginx 关键字
upstream backend {  #服务器集群名字  
    ip_hash;
    server    localhost:8080;# 服务器配置    
    server    localhost:18080;# 服务器配置  
}

权重负载平衡

权重能够应用在轮询、最小连接数、ip-hash 中

upstream backend {  #服务器集群名字  
    server    localhost:8080 weight=3; #服务器配置    
    server    localhost:18080;# 服务器配置  
}

这个配置,当有四个申请的时候,第一个服务器解决 3 个,第二个解决 1 个

配置 https 服务

生成 https 证书形式

点我取得 生成 https 证书形式的详细信息

sudo mkdir /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

配置 https 服务

ssl 参数必须启用,证书和私匙必须指定,配置如下

    # HTTPS server
    #
    server {
        listen       443 ssl;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        # 前缀 "/" 会匹配所有申请,如果有多个 location 匹配一个申请,则抉择前缀最长的
        location / {
            #每个申请都会加上 root 对应的地址,就是文件在服务器的真是地址
            root /data/fptxl;
            #默认关上网页
            index fptxl.html;
        }
        
        location /fptxl/ {proxy_pass http://backend/;}
    }

留神:编码时候,尽量不要指定协定头,能够从通过 js(window.location.protocol)中获取,缩小 http 转化为 https 时协定不统一引起的异样。

websocket 配置

HTTP 1.1 反对将 HTTP/1.1 协定转化为 WebSocket

nginx 配置如下

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        location /chat/ {
            proxy_pass http://chat/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_read_timeout 6000;
            proxy_set_header Origin "";
        }
    }

websocket 的 wss 配置

wss 与 ws 的区别在于一些 ssl 的配置,与 http 和 https 的区别统一

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        listen       8443 ssl;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
    
        location /chat/ {
            proxy_pass http://chat/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_read_timeout 6000;
            proxy_set_header Origin "";
        }
    }

wss 和 https 是能够共享 443 端口的。

这里不共享一个端口是因为 wss 的我的项目和 https 的我的项目是两个我的项目,动态文件地址不能合并在一起

nginx 局部指令解释

location:nginx 抉择 location 为申请提供服务,优先应用正则,其次应用最长前缀。

root:申请的根目录,一个残缺的申请是 root+uri,如果以后指令中没有 root, 则会应用上下文的 root. 如下配置,申请 /fptxl.html 的返回文件为 /data/fptxl/fptxl.html。

location /fptxl.html {root /data/fptxl;}

申请 ”/i/top.gif” 的响应是文件 ”/data/w3/i/top.gif”, 如果 location 没有 root 指令,则会应用 server 中配置的 root 指令

proxy_pass: 设置被代理服务的协定、地址和 uri,地址能够是域名或者 ip 和一个可选的端口

演示中的 nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {worker_connections  1024;}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '
    #                  '$status $body_bytes_sent"$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    #websocket 配置时应用
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    #名称尽量不要应用特殊字符,防止应用了 nginx 关键字
    upstream backend {  #服务器集群名字  
        server    localhost:8080; #服务器配置    
        server    localhost:18080;# 服务器配置  
    }
    
    #名称尽量不要应用特殊字符,防止应用了 nginx 关键字
    upstream talk {  #服务器集群名字  
        server    localhost:8090; #服务器配置    
        server    localhost:8091;# 服务器配置  
    }
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        # 前缀 "/" 会匹配所有申请,如果有多个 location 匹配一个申请,则抉择前缀最长的
        location / {
            #每个申请加上 root 的地址,就是文件在服务器的实在地址
            root /data/fptxl;
            #默认关上网页
            index fptxl.html;
        }
        
        location /fptxl/ {proxy_pass http://backend/;}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {root   html;}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    
    server {
        listen       8088;
        server_name  localhost;
        
        location / {
            proxy_pass http://talk;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Origin "";
        }

    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443 ssl;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        # 前缀 "/" 会匹配所有申请,如果有多个 location 匹配一个申请,则抉择前缀最长的
        location / {
            #每个申请加上 root 的地址,就是文件在服务器的实在地址
            root /data/fptxl;
            #默认关上网页
            index fptxl.html;
        }

        location /fptxl/ {proxy_pass http://backend/;}
        
    }
    
    server {
        listen       8443 ssl;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        location / {
            proxy_pass http://talk;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Origin "";
        }
        
    }

}

配置文件的服务器为 www.fptxl.com, 能够间接拜访,但因为是测试服务器,随时会生效

其它

问题:前端请代理中的后端申请地址,为什么不间接应用后端服务器的地址?参考答案:后端负载平衡。

正文完
 0