1. Nginx 下载(以1.21.6为例)

下载链接

2. 解压

使用ftp工具上传到/opt路径下, 用下面的命令解压

tar -zxvf nginx-1.21.6.tar.gz

3. 编译和安装

解压后的/opt/nginx-1.21.6/目录下有一个 config 的文件, 执行该文件

./config
# 如果需要开启ssl模块,需要执行以下命令(ssl模块开启之后才能通过https访问)
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

完成后分别执行make 和make install 命令安装nginx

make && make install

nginx安装前需要gcc 和zlib依赖等,,,

4. 常用命令

跳转到nginx 的安装目录(默认:/usr/local/nginx/sbin)

./nginx -v #查看版本号
./nginx # 启动
./nginx -s stop # 停止
./nginx -s status # 查看状态
./nginx -s reload # 重启/重新加载配置
./nginx -s quit # 处理完成当前请求之后冠以,并且不再处理新的请求

5. 安装服务 并设置开机自启动

执行以下命令,将nginx安装成系统服务

vi /usr/lib/systemd/system/nginx.service

在服务文件中粘贴以下内容:

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

保存后可以使用操作服务的方式操作nginx

systemctl start nginx.service # 启动nginx
systemctl stop nginx.service # 停止nginx
systemctl daemon-reload nginx.service # 重启nginx
systemctl enable nginx.service # 开机自启动

6. nginx配置文件

路径(默认): /usr/local/nginx/conf

全局配置:

worker_processes 1;

nginx启动时会有两个进程(不是线程),一个是master线程,读取配置并协调各个工作进程.并不会对客户端发来的请求进行处理.
对于客户端发来的请求都是由work进程进行处理的, 而worker_processes就是在设置work进程的数量.(最好跟处理器核心数一致)

events配置

worker_connections 1024;

nginx服务器跟用户的最大连接数

http配置
  • http全局配置
    include mime.types;

引入http mime类型(告诉浏览器这是什么类型的文件,以便浏览器可以自动打开展示给用户)

  • default_type application/octet-stream;

如果mime类型没匹配上,默认使用二进制流的方式传输(用户在浏览器里看到的就是文件没有被打开而是被下载)。

  • server配置
    listen 80;

监听端口

  • server_name localhost;

主机名,默认是localhost,如果有,可以替换为自己的域名

  • location /

可以配置多个,根据客户端的请求路径配置不同路径,最终返回给客户端不同的文件.支持正则

  • sendfile on;

使用linux的 sendfile方法直接发送文件给客户端,实现文件的高效传输.否则文件会先被复制到nginx服务器的内存中再发给客户端,增加服务器的压力.

7 简单配置反向代理

7.1 启动两个端口号分别为8080和8081的tomcat服务(通过docker启动较为简单,过程略)

为便于区分,需要分别在两个Tomcat上面创建两个html文件,以便后面确认结果

7.2 修改nginx.conf配置文件

修改server配置

server {
        listen       80;
        server_name  本机IP;
        location ~/edu/ {
            root   /www;
            proxy_pass http://127.0.0.1:8081;
            index  index.html index.htm;
        }
        location ~/vod/ {
            root   /www/www;
            proxy_pass http://127.0.0.1:8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

测试: 在浏览器输入下面两个url会出现两个不同的网页,表示配置完成
http://主机ip:端口/vod/index.html
http://主机ip:端口/edu/index.html

8 配置负载均衡

8.1 分别启动两个端口号为8080和8081的服务.

需要在/tomcat/webapps/ 文件夹下新建两个相同的文件夹,然后分别创建两个名称相同内容不同的html文件

8.2 修改nginx.conf配置文件

  • 在server平级的区域配置负载均衡器
# loadbalance配置
    upstream mylb{
        # 负载均衡的方式(轮询(默认),IP哈希(IP_HASH),权重(weight=),url哈希(url_hash),最少连接(least_conn),fair)
        server 本机IP:8080;
        server 本机IP:8081;
    }
  • 在localtion中引用负载均衡器
location ~/edu/ {
            root   /www/www;
            proxy_pass http://mylb;
            index  index.html index.htm;
        }

测试: 默认会通过轮询的方式来实现负载均衡,输入以下url,每此刷新都能看到不同的页面
http://主机ip/edu/index.html

9 配置动静分离

9.1 在服务器根目录创建一个测试用的文件夹,在里面放入测试用的文件

nginx conf 不等于_nginx

9.2 修改nginx.conf配置文件的内容

location /images/ {
            root   /www;
            autoindex on;
        }

在浏览器输入以下url即可访问到静态资源

http://主机IP/images/grinning-face-with-sweat_1f605.png

nginx conf 不等于_IP_02