# nginx小记

  • 本次学习nginx使用的是Ubuntu系统

# install nginx

sudo apt-get install nginx
1

# nginx的常用命令

# 启动命令

  • 直接输入nginx
  nginx
1
  • 还可以通过systemctl来开启
systemctl start nginx.service
1

# 停止nginx

# 立即停止

nginx -s stop
1

# 优雅的停止

nginx -s quit
1

# kill 结束nginx进程

  • 先查看nginx占用的PID
  lsof -i:80
1
  • 输出如下内容,找到nginx进程的PID
COMMAND     PID     USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
nginx     16903     root    6u  IPv4 645602965      0t0  TCP *:http (LISTEN)
nginx     16903     root    7u  IPv6 645602966      0t0  TCP *:http (LISTEN)
nginx     16904 www-data    6u  IPv4 645602965      0t0  TCP *:http (LISTEN)
nginx     16904 www-data    7u  IPv6 645602966      0t0  TCP *:http (LISTEN)
1
2
3
4
5
  • 使用kill命令结束进程
kill -9 16903 && kill -9 16904
1
  • 也可以使用 killall 命令结束nginx进程
killall nginx
1

# 运行第一个静态网站

# 配置文件介绍

  • 主配置文件
/etc/nginx/nginx.conf
1
  • 子配置项,一般我们没有做任何的定制,子配置项会在
/etc/nginx/conf.d/default/*.conf
1

# 配置编写

  • 首先准备好静态页面,这里我用的是我自己开发的粒子动画展示网站-cfazy-future
  • 将打包好的文件发送到服务器的 /data/blog/dist目录下
  • 修改nginx的配置,在/etc/nginx/conf.d/default.conf中写入一下配置
server {
      listen 80;
      server_name www.woniuwnwn.top;
      location / {
              root /data/blog/dist;
              index index.html index.htm;
      }
}

1
2
3
4
5
6
7
8
9
  • 退出保存重启nginx -s reload,如果之前没有启动,直接nginx启动即可
  • 点击图片预览 预览

TIP

  • 需要注意的是 server_name 要写对域名,否则直接输入域名访问不了,还需要再域名后面配上 /index.html
  • eg: http://www.woniuwnwn.top/index.html
  • version : nginx version: nginx/1.14.0 (Ubuntu)

# 纯ipv6服务器

  • 需要在原配置上增加一行listen [::]:80
server {
      listen 80;
      listen [::]:80;
      server_name www.woniuwnwn.top;
      location / {
              root /data/blog/dist;
              index index.html index.htm;
      }
}

1
2
3
4
5
6
7
8
9
10

# 指定错误页面

  • 通过error_page来指定
server {
      listen 80;
      server_name www.woniuwnwn.top;
      error_page /xx/xx/404.html
      location / {
              root /data/blog/dist;
              index index.html index.htm;
      }
}
1
2
3
4
5
6
7
8
9
  • error_page后面也可以指定网址,如果资源未找到,直接跳转网址
server {
      listen 80;
      server_name www.woniuwnwn.top;
      error_page www.woniuwnwn.top/error;
      location / {
              root /data/blog/dist;
              index index.html index.htm;
      }
}
1
2
3
4
5
6
7
8
9

# gzip压缩开启

  • 首先进入nginx的主配置文件
cd /etc/nginx && vim nginx.conf
1
  • 在http配置中增加gzip的属性并开启
http{
  gzip on;

  # other config
}
1
2
3
4
5

# 权限

# deny和allow

  • 通过deny和allow配置拒绝和可访问的ip
server {
      listen 80;
      server_name www.woniuwnwn.top;
      location / {
              root /data/blog/dist;
              index index.html index.htm;
              deny xx.xx.xx.xx/xx;
              allow xx.xx.xx.xx/xx;
      }
}
1
2
3
4
5
6
7
8
9
10
  • denu 和 allow 后面的 xx/xx代表一段连续的ip拒绝和允许访问
location / {
        root /data/blog/dist;
        index index.html index.htm;
        deny 192.168.1.5/100; # 表示拒绝从5~100的IP访问
}
1
2
3
4
5

# deny和allow的优先级

  • 写在上面的优先级要高,所以说下面这个例子是,允许192.168.1.5/100这些ip访问的,deny无效
location / {
        root /data/blog/dist;
        index index.html index.htm;
        allow 192.168.1.5/100; # 表示允许从5~100的IP访问
        deny 192.168.1.5/100; # 表示拒绝从5~100的IP访问
}
1
2
3
4
5
6

# location匹配

# 精确匹配 “=”

  • = 匹配的优先级最高
  • 不允许用户访问后台管理
location =/back-stage {
        root /data/blog/dist;
        index index.html index.htm;
}
1
2
3
4

# 精确匹配 “^~”

  • ^~ 匹配的优先级次于=
location ^~/back-stage {
        root /data/blog/dist;
        index index.html index.htm;
}
1
2
3
4

# 正则匹配 “~”

  • ~ 匹配的优先级次于^~
location ~\.mov$ {
        root /data/blog/dist;
        index index.html index.htm;
}
1
2
3
4

# 匹配任何查询

  • 优先级最低
location / {
        root /data/blog/dist;
        index index.html index.htm;
        allow all;
}
1
2
3
4
5

# 设置虚拟主机

# 通过域名设置虚拟主机

  • 需要在准备一个域名,我们可以在解析一个子域名 我这里新解析了一个test.woniuwnwn.top域名
  • 在准备一个静态页面
cd /data/test && mkdir index.html
1
echo '<h1>test</h1>' >> index.html
1
  • 在上面的例子中已经在conf.d目录下有一个default.conf子配置项,这里我们要新建一个
cd /etc/nginx/conf.d && vim test.conf
1
  • 新建好文件之后在里面在配一套server
server {
      listen 80;
      server_name test.woniuwnwn.top;
      location / {
              root /data/test;
              index index.html index.htm;
      }
}
1
2
3
4
5
6
7
8
  • 此时在重启nginx就可以通过test.woniuwnwn.top访问我们配置的虚拟主机的项目了
nginx -s reload
1

# 反向代理

  • 正向代理是给客户端做代理
  • 反向代理是给服务器做代理

# proxy_pass 反向代理

server {
    listen 80;
    server_name test.woniuwnwn.top;
    location / {
            proxy_pass xx.xx.xx.xx:xxxx;
    }
}
1
2
3
4
5
6
7
  • 反向代理还有很多配置可以看nginx的官网传送门

# 将/test匹配代理到目标域名的/配置技巧

server {
    listen 80;
    server_name test.woniuwnwn.top;
    location /test/ {
            proxy_pass xx.xx.xx.xx:xxxx/;
    }
}
1
2
3
4
5
6
7
  • 由上面这个例子可以看出,技巧就是将/test后面加上/,在proxy_pass的代理域名后面也加一个/

# 适配PC或mobile设备,来跳转不同域名

# 通过$http_user_agent来做判断

server {
    listen 80;
    server_name www.woniuwnwn.top;
    location / {
            root /data/pc;
            if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
              root /data/mobile;
            }
            index index.html index.htm;
    }
}
1
2
3
4
5
6
7
8
9
10
11

TIP

  • 这个自适应是nginx帮我们进行不同平台进行不同页面的加载
  • 和前端的的自适应不是一回事,通过nginx可以帮助前端来做适配,就不用把适配的代码都写到一个工程里面了,相当于减小包体积,增加加载速度,减小白屏时间

# 负载均衡

# upstream

  • 简单配置一个负载均衡的小案例
upstream group1{
        server xx.xx.xx.xx.端口1;
        server xx.xx.xx.xx.端口2;
}
server {
    listen 80;
    server_name www.woniuwnwn.top;
    location / {
            proxy_pass http://group1
    }
}
1
2
3
4
5
6
7
8
9
10
11

# weight 负载均衡加权

# 服务器性能好设置权重大一些,利于资源分配合理化

upstream group1{
        server xx.xx.xx.xx.端口1 weight = 1;
        server xx.xx.xx.xx.端口2 weight = 1;
}
1
2
3
4

# 相关

  • OpenResty 基于 NGINX 和 LuaJIT 的 Web 平台。
Last Updated: 1/23/2022, 10:16:22 AM