Nginx学习及工作中常用的一些操作

在这里插入图片描述

一、Nginx版本区别

  • Nginx开源版
    • http://nginx.org
  • Nginx plus 商业版
    • https://www.nginx.com
  • openrstry
  • Tengine
    • http://tengine.taobao.org/

二、Nginx安装

下载地址:nginx: download

==安装前准备工作

  1. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++
  1. pcre pcre-devel 安装
pcre(perl compatible regular expressions) 是一个perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令

yum install -y pcre pcre-devel

  1. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 centos 上安装 zlib 库。

yum install -y zlib zlib-devel
  1. openssl 安装
	openssl 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 ssl 协议,并提供丰富的应用程序供测试或其它目的使用。  
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 centos 安装 openssl 库。

yum install -y openssl openssl-devel

2.1. 上传压缩包至服务器

cd /usr/local/temp

2.2. 解压缩Nginx文件

tar -zxvf nginx.1.20.2.tar.gz

2.3. 编译安装

./configure --prefix=/usr/local/nginx
make 编译
makle install 安装

安装过程中会出现error错误,请参考文档下面的注意事项

2.4 配置开机自启动

即在rc.local增加启动代码就可以了。
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
设置执行权限
chmod 755 rc.local

2.5. 启动Nginx 相关命令

进入 cd /usr/local/nginx/sbin
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭Nginx, 在退出前完成已经接受的请求
./nginx -s restart 重启
./nginx -v 查看版本号
./nginx -V 查看Nginx都是配置了哪些模块

2.6. 如果访问Nginx不成功

  • 方式一
    默认防火墙如果开启,则会访问不到Nginx,我们需要关闭防火墙
    systemctl stop firewalld.service 关闭防火墙
    systemctl disable firewalld.service 禁止防火墙开启自启动
    firewall-cmd --reload 重启防火墙
  • 方式二
    放行端口
    firewall-cmd --zone=public --add-port=80/tcp --permanent

2.6. 安装成系统服务

创建服务脚本

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

重新加载系统服务
systemctl daemon-reload

启动服务
systemctl start nginx.service

开机启动
systemctl enable nginx.service

重新加载配置
systemctl reload nginx.service

查看错误详情
systemctl status nginx.service -l

2.7. 安装过程中遇到的问题

2.7.1 问题一

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

安装perl库: yum install -y pcre pcre-devel

2.7.2 问题二

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

安装zlib库: yum install -y zlib zlib-devel

代理服务器的安全问题

三、一台机器配置多个Nginx

要求配置两台Nginx,负载均衡到每台主机请求,
nginx-88
nginx-89

3.1 拷贝配置文件

cd /usr/local/nginx/conf
cp nginx.conf nginx-88.conf
cp nginx.conf nginx-89.cong

3.2 修改88,89配置文件

  1. 更改linsen监听端口80为89,88
  2. location 中的root根目录变更为自己定义的目录
  3. 在root指定的根目录下面分别创建88,89所对应的HTML页面

3.3 修改80配置文件

  1. 增加upstream 负载均衡配置
    upstream httped{
    server 127.0.0.1:88 weight=1;
    server 127.0.0.1:89 weight=10;
    }

  2. 修改location 模块
    这时候需要删掉location模块下面的默认配置
    修改为proxy_pass http://httped;
    注意:
    http://httped 这块内容一定要和上面的upstream后面的字符串保持一致

  3. 分别启动三个配置文件即可

3.4 在启动另外两个Nginx的时候遇到如下错误

-c 是指定启动那个文件的命令,这时候linux报如下错误

./nginx -c conf/nginx-88.conf

ginx: [emerg] open() "/usr/local/nginx//../conf/nginx-88.conf" failed (2: No such file or directory)
说明Nginx他不认上级命令的操作,需要把当前的命令改成如下:

./nginx -c /usr/local/nginx/conf/nginx-88.conf

四、负载均衡,Https配置声明

4.1 权重

weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

upstream httpds {
    server 127.0.0.1:8050       weight=10 down;
    server 127.0.0.1:8060       weight=1;
     server 127.0.0.1:8060      weight=1 backup;
}
  • down 表示当前的server的暂时不参与负载
  • weigth 默认为1.weight越大,负载的权重就越大
  • backup 其它所有的非backup机器down或者忙的时候,请求backup机器。

4.2 Https

这块没有外网的主机,没有实际操作
总结一下几个步骤:
1、需要申请域名,配置域名
2、申请证书,腾讯云,阿里云都可以
3、申请证书网站有示例,有一个环节需要按照提示在Nginx根目录下面创建对应的文件和私钥
4、官网会去你配置好的根目录下自动验证,这个环节主要看下域名是不是你本人的
5、吧证书放在根目录下面后,在nginx.conf的配置文件中配置https模块内容

server {
	 listen       443 ssl;
	 server_name  aa.abc.com;
	 ssl_certificate      /data/cert/server.crt;
	 ssl_certificate_key  /data/cert/server.key;
     }

4.2.1 免费签名

https://freessl.cn

五、在公网配置Https

Nginx配置

  server {
	  
	 listen       443 ssl;
	 
	 server_name  aa.abc.com;
	
	 ssl_certificate      /data/cert/server.crt;
	 
	 ssl_certificate_key  /data/cert/server.key;
     }

5.1 参考实例

server {
    #SSL 访问端口号为 443
    listen 443 ssl;
	 #填写绑定证书的域名
    server_name duozuiyu.com;
	 #证书文件名称
    ssl_certificate duozuiyu.com.crt;
	 #私钥文件名称
    ssl_certificate_key duozuiyu.com.key;
    location / {
	    #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
        root html;
        index  index.html index.htm;
    }
}

5.1.1 配置Https编译的时候报错问题

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:98
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
5.1.1.1 解决方法
  1. 重新编译 增加ssl模块
	./configure --with-http_stub_status_module --with-http_ssl_module
  1. 执行 make. ==切记不要执行make install

  2. 重启Nginx

六、Nginx动静分离配置

6.1 配置Nginx的反向代理

 location / {
	proxy_pass http://127.0.0.1:8080;
	root   html;
	index  index.html index.htm;
}

6.2 增加每一个location

location /css {
         
	root   /usr/local/nginx/static;
	index  index.html index.htm;
}
  location /images {
 
	root   /usr/local/nginx/static;
	index  index.html index.htm;
}

  location /js {
	 
	root   /usr/local/nginx/static;
	index  index.html index.htm;
}

我们也可以用正则表达式来配置一个location,达到上面的效果

location ~*/(css|images|js) {
     
        root   /usr/local/nginx/static;
        index  index.html index.htm;
}

6.3 location配置详解

location 前缀


没有前缀 匹配以指定模式开头的location


= 精准匹配,不是以指定模式开头


~ 正则匹配,区分大小写


~* 正则匹配,不区分大小写


^~ 非正则匹配,匹配以指定模式开头的location 优先级高

location匹配顺序

  • 多个正则location直接按书写顺序匹配,成功后就不会继续往后面匹配

  • 普通(非正则)location会一直往下,直到找到匹配度最高的(最大前缀匹配)

  • 当普通location与正则location同时存在,如果正则匹配成功,则不会再执行普通匹配

  • 所有类型location存在时,“=”匹配 > “^~”匹配 > 正则匹配 > 普通(最大前缀匹配)

6.3.1 alias与root

示例

location /css {
     
        alias   /usr/local/nginx/static/css;
        index  index.html index.htm;
    }

root用来设置根目录,而alias在接受请求的时候在路径上不会加上location

  1. alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;

  2. root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;

  3. 使用alias标签的目录块中不能使用rewrite的break(具体原因不明);另外,alias指定的目录后面必须要加上"/"符号!!

  4. alias虚拟目录配置中,location匹配的path目录如果后面不带"/“,那么访问的url地址中这个path目录后面加不加”/“不影响访问,访问时它会自动加上”/“; 但是如果location匹配的path目录后面加上”/“,那么访问的url地址中这个path目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/",访问就会失败!

  5. root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值