nginx 服务器的安装和配置

2,125 阅读4分钟

nginx 能做什么 ?

像之前部署前端静态资源一般都选择 apache,现在选择 nginx,那 nginx 都能做什么呢?

  • 静态路由
  • 反向代理
  • 负载均衡

安装

  1. 访问 nginx 官网

nginx

  1. 选择 nginx 版本

这里选择的是 nginx-1.17.9

  1. 查看下面的 Pre-Built Packages 部分

点击 stable and mainline

  1. 选择 centos 版本,复制对应的 yum 配置
vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

  1. 安装

yum install nginx

配置

** 说明 **

  • nginx 的主配置文件一般是不需要动的,我们只需要在 /etc/nginx/conf.d 目录下新建自己的配置文件即可,因为在主配置文件 /etc/nginx/nginx.conf 文件的最后有这么一句 include /etc/nginx/conf.d/*.conf;,会自动将 conf.d 目录下的配置文件引入

  • 如果 nginx 出现 403 提示权限不够的情况,一般是主配置文件的 user 信息有问题(和静态资源文件的所有者不一致),或者 url 最后访问到的资源路径和你预想的不一致(确实没有权限)

  • 做完配置以后,可以执行 nginx -t验证配置内容是否有误

  • 执行 service nginx start 启动 nginx

  • nginx -s reload 可实现 nginx 的重新载入

新建自己的应用配置文件

# vi /etc/nginx/conf.d/my.conf
server {
  # 应用的端口
  listen 8081;
  #server_name myApp.test.com;
  
  # 静态路由
  location / {
    root /front-end/myApp;
    index index.html index.htm;
  }
  location ~ \.(gif|jpg|png)$ {
    root /front-end/myApp;
  }
  
  # 反向代理
  location /api {
    proxy_pass http://localhost:3000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Fox $proxy_add_x_forwarded_for;
  }
  location /socket.io {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
  }
}

拷贝你打包后的静态资源到 /front-end/myApp 目录下,即你打包的资源目录就是 myApp,启动 nginx,在浏览器访问 http://ip:8081,如果前面都没错,则可以看到你的应用

常用配置解释

# 匹配根路径,最后时间访问的路径为:/front-end/myApp/
location ^~ / {
  root /front-end/myApp;
}

# 匹配以/portal开始的路径,且忽略大小写,最后实际访问的地址: /front-end/myApp/portal
location ^~ /portal {
  root /front-end/myApp;
}

# 还是匹配以 /portal 开始的路径,且忽略大小写,但最后访问的地址是:/front-end/myApp
location ^~ /portal {
  alias /front-end/myApp;
}

# 按照 alias 的处理逻辑,在最终的访问路径中没有找到资源时,会按照 try_files 指令的规则去匹配
location ^~ /portal/overview {
  alias /front-end/myApp;
  try_files $uri $uri/ /;
}

root 和 alias 的区别

rootalias的区别简单来说就是在处理最终访问路径时的规则不一样。

  • root,root 后面的字符串 + location 后的地址 => 实际访问地址

  • alias,alias 后面的字符串 => 实际访问的地址

try_files

try_filesnginxhttp_core核心模块所带的指令,主要是能替代一些rewrite的指令,提高解析效率。官网的文档

语法规则

  • 格式

    try_files uriuri/ /xxx

    try_files uriuri/ /xxx=code

  • try_files 第一个参数是上下文

    server + location

参数说明

  • try_files 上下文 server/location,比如: http://location:8080/portal
  • $uri 表示上下文
  • $uri/ 表示上下文中这个目录,这个参数一定要有,不然不会去找上下文目录下的 index

举例说明:

location /images/ {
    root /opt/html/;
    try_files $uri $uri/ /images/default.gif; 
}

请求 127.0.0.1/images/test.gif 会依次查找

  • /opt/html/images/test.gif
  • /opt/html/images/test.gif/下的index文件
  • 127.0.0.1/images/default.gif

4.其他注意事项 1.try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页 即 访问127.0.0.1/images/ 不会去访问 127.0.0.1/images/index.html

配置 GZip 压缩

# vim /etc/nginx/nginx.conf

...
http {
  ...
  # 启用 nginx 的动态压缩功能,每次收到请求,都需要对涉及到的资源进行压缩处理
  # gzip on;
  
  # 启动 nginx 的静态压缩功能,如果收到的请求支持 gzip 压缩,则返回以 .gz 结尾的资源文件代替普通文件,如果请求不支持 gzip,则返回普通文件,好处是不同服务器每次动态压缩,降低服务器的负荷
  gzip_static on;
  
  # 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩
  gzip_min_length 1k;

  # gzip 压缩级别 1-10 
  gzip_comp_level 2;

  # 进行压缩的文件类型,一般配合动态压缩使用,静态压缩设置 gzip_types 没用
  # gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

  # 是否在http header中添加Vary: Accept-Encoding,建议开启
  gzip_vary on;
  ...
}
...

配置缓存

** 说明 **

缓存可分为 强缓存 和 弱缓存 两种 基于上面的配置,我们打开浏览器的控制台,一通分析之后,发现 nginx 默认为静态资源都配置了弱缓存

弱缓存

而我们通用的方案一般是只需要 html 是弱缓存即可,其它像各种图片、js、css文件等,都可以设置为强缓存,因为若缓存还是会像服务器发送一次验证请求,验证资源是否过期,挺费时的

设置强缓存

# vim /etc/nginx/conf.d/my.conf

server {
  ...
  location ~ \.(gif|jpe?g|png)$ {
    # 图片设置强缓存
    root /front-end/myApp;
    add_header Cache-Control max-age=360000;
  }
  location ~ \.(js|css)$ {
    # js、css设置强缓存
    root /front-end/myApp;
    add_header Cache-Control max-age=360000;
  }
  ...
}

** 效果 **

强缓存

高可用

使用场景

  • 负载均衡

当用户量比较大时,服务器的负载就会比较高,单台服务器可能就有点搞不定了,这时候我们就需要进行横向扩展,多增加几台 nginx 服务器,即配置负载均衡,通过配置权重的方式,将这些用户请求分发到不同的服务器上,可以避免单台服务器出现高负载崩溃的情况

  • 容灾

其实配置了负载均衡,也就有了容灾的能力。容灾是指如果有某个服务器挂掉了,其它服务器可以继续顶上,不会出现服务不可用的情况,配了负载均衡,如果有某个服务器挂了,会将请求分发到其它可用的服务器上。

实践

我们准备三台 nginx 服务器,分别命名 slb(负载均衡服务器)、server1、server2

  • slb
# vim /etc/nginx/conf.d/slb.conf

upstream roundrobin {
  # 配置 nginx 服务器的访问规则,即权重
  server server1:80 weight=2; # server1
  server server2:80 weight=1; # server2
}

server {
  listen 80;
  location / {
    # 设置反向代理,将流量都代理到其它服务器
    proxy_pass http://roundrobin;
  }
}

  • server1
# 内容就是就是普通的 nginx 服务器的配置
  • server2
# 内容就是就是普通的 nginx 服务器的配置