Nginx

本文详细介绍了Nginx的用途、特性及安装过程,包括作为HTTP和反向代理服务器的角色,以及如何进行静态资源部署和设置反向代理。通过Docker安装Nginx并配置虚拟主机,展示了Nginx在负载均衡和动静分离方面的应用。此外,还探讨了Nginx配置文件的解析,帮助读者理解如何定制和管理Nginx服务器。
摘要由CSDN通过智能技术生成

背景

  • 网站上线初期,用户量小,并发量小,只需要一台服务器即可。
    请添加图片描述

  • 随着用户量增加,一台服务器已经不能适应需要 -> 横向扩展服务器。中间件做反向代理 -> nginx

架构:没有什么是加一层解决不了的。如果不行就加两层。

什么是nginx

nginx是一个高性能的HTTP反向代理服务器。同时也提供了IMAP/POP3/SMTP服务。

特点:占有内存小,并发能力强!

nginx作用

Http代理,反向代理:作为web服务器最常用的功能,尤其是反向代理。

  • 正向代理:代理客户端(类似本地VPN)–代替客户端向服务器发送请求。

在这里插入图片描述

  • 反向代理:代理服务器 – 代替服务器接收客户端的请求,并将请求转发到真正响应的服务器上。

在这里插入图片描述

负载均衡。Nginx提供的负载均衡策略有两种:内置策略和扩展策略。

内置策略:

  • 轮询
  • 加权轮询
  • Ip hash

扩展策略:只有想不到的没有做不到的。

动静分离

在开发中,有些请求需要动态处理,有些不经过后台处理,比如静态资源css、html、jpg、js等等文件。让动态网站里的动态网页根据一定规则把不变的资源和经常改变的资源区分开来,动静资源做好了拆分之后,我们就可以根据静态资源的特点将其做缓存操作,提高资源的响应速度。

在这里插入图片描述

Nginx安装

通过Docker安装

拉取镜像

docker pull nginx

创建容器,将相关配置文件拷贝出来

# --rm 声明该容器退出后自动销毁
[root@aliyunECS ~]# docker run -it --rm nginx /bin/bash
root@a7ebe90d6b63:/# pwd
/
root@a7ebe90d6b63:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@a7ebe90d6b63:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx

# 启动另一个ssh窗口,将配置文件拷贝到宿主机
[root@aliyunECS ~]# docker cp a7ebe90d6b63:/etc/nginx/nginx.conf ~/nginx-test/
[root@aliyunECS nginx-test]# docker cp a7ebe90d6b63:/etc/nginx/conf.d ~/nginx-test/
[root@aliyunECS nginx-test]# docker cp a7ebe90d6b63:/usr/share/nginx/html ~/nginx-test/
[root@aliyunECS nginx-test]# ls
conf.d  html  nginx.conf
[root@aliyunECS nginx-test]# cd html
[root@aliyunECS html]# ls
50x.html  index.html

相关文件都拷贝到了~/nginx-test文件夹中了,这就是后面数据卷挂载的目录。

/etc/nginx/nginx.conf内容:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

观察nginx.conf可知:

  • /etc/nginx/nginx.conf是nginx的核心配置文件!该文件最后的include /etc/nginx/conf.d/*.conf; 声明将conf.d目录下的所有*.conf文件中的配置引入到这里。
  • /usr/share/nginx是保存nginx数据的地方,其中有一个默认的html文件夹,是nginx做http代理时的默认根目录(相当于Tomcat里的webapps/ROOT目录)

/etc/nginx/conf.d/default.conf内容:

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/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;
    #}
}

default.conf就是我们后续自己配置虚拟服务器server的模板。

启动新的容器,绑定端口,挂载数据卷

[root@aliyunECS ~]# docker run -id -p 80:80 \
> -p 81:81 \
> -p 82:82 \
> -v ~/nginx-test/conf.d:/etc/nginx/conf.d \
> -v ~/nginx-test/nginx.conf:/etc/nginx/nginx.conf \
> --name=c_nginx_test \
> nginx
431d911e534644f5ce2e121e55229cb6531231cbbfad530c5f56a7e6ee4ab183
[root@aliyunECS ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
431d911e5346   nginx     "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   0.0.0.0:80-82->80-82/tcp, :::80-82->80-82/tcp   c_nginx_test

测试

访问localhost:80

在这里插入图片描述

Nginx 虚拟主机配置解析

观察上述的default.conf文件,里面配置了一个虚拟主机

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/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;
    #}
}

listen

监听端口

server_name

匹配顺序
server_name与host匹配优先级如下:

1、完全匹配

2、通配符在前的,如*.test.com

3、在后的,如www.test.*

4、正则匹配,如~^.www.test.com$

如果都不匹配

1、优先选择listen配置项后有default或default_server的

2、找到匹配listen端口的第一个server块

location:匹配路径

匹配规则

location [=|~|~*|^~|@] /uri/ {
  root ...   # 根目录
  index ...  # index文件
  proxy_pass ... # 反向代理地址
} 
  • = : 表示精确匹配后面的url
  • ~ : 表示正则匹配,但是区分大小写
  • ~* : 正则匹配,不区分大小写
  • ^~ : 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
  • @ : “@” 定义一个命名的 location,使用在内部定向时,例如 error_page

上述匹配规则的优先匹配顺序:

  1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索;
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^~ 前缀,搜索停止;
  3. 正则表达式,在配置文件中定义的顺序;
  4. 如果第 3 条规则产生匹配的话,结果被使用。否则,使用第 2 条规则的结果。

uri处理规则

处理规则简单一句话:如果proxy_pass后面没有任何URL路径信息(比如/,/xxx等),则反向代理的地址会包含location中的匹配部分,否则只会拼接匹配后的剩余路径

参考:https://cloud.tencent.com/developer/article/1407722

静态资源部署

  • 自定义一个server配置,定义root、index等信息
  • 把war包解压后放到上述定义的root目录里

反向代理设置

  1. 在conf.d中自定义一个xxx.conf文件,内容包括:upstream、server两部分
  2. upstream定义:
upstream cluster {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
}

以上定义了三台虚拟服务器,通过cluster包装在一起。后续访问http://cluster就能直接以负载均衡的方式访问这三台主机中的一台。

  1. server里定义:
server {
     listen 80; # 监听的端⼝
     server_name www.kkb.com; # 域名或ip
     location / { # 访问路径配置
         # root index;# 根⽬录
         proxy_pass http://cluster;
         # index index.html index.htm; # 默认⾸⻚
     }
}

如果直接部署静态资源,则定义root和index,分别表示静态资源的根目录和访问首页。如果是反向代理,则定义proxy_pass表示代理到哪个网址。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值