[Nginx 安装,https 模块安装,Web 和反向代理服务器 配置]

65 阅读5分钟

[Nginx 安装,https 模块安装,Web 和反向代理服务器 配置]

[微学网络]

Nginx 是一款高性能的 Web 和反向代理服务器,今天我们就在 MacOS(Mojave,版本 10.14.6)环境下学习下 Nginx。

一、安装、启动、重启、停止 Nginx (方式一)

  • Mac 环境的安装比较简单,使用 Homebrew 安装即可:
 brew install nginx 

安装后的路径如下:

/usr/local/Cellar/nginx
/usr/local/etc/nginx

启动 Nginx 服务:

sudo nginx

二、linux 安装、启动、重启、停止 Nginx (方式二):

  • 备注: linux 系统为 Centos 64 位

第一步:从 nginx.org/download/ 上… nginx.org/download/ng…)

第二步:解压 tar -zxvf nginx-1.17.1.tar.gz

第三步:设置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不执行此步,直接默认配置 备注:https 方式: ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

第四步:

make 编译 (make 的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

make install 安装 (make install 是把这些编译出来的可执行文件和库文件复制到合适的地方)

在配置信息的时候,也就是在第三步,出现了一下错误:

checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

./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.

[root@VM-0-14-centos nginx-1.18.0]# make install 

错误为:./configure: error: the HTTP rewrite module requires the PCRE library.

安装 pcre-devel 解决问题 yum -y install pcre-devel

还有可能出现:

  • 错误提示:
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
  • 解决办法:

yum -y install openssl openssl-devel

  • 安装后在 linux 下启动和关闭 nginx:

验证下是否启动成功,在浏览器打开 http://localhost:8080/
Nginx 默认端口为 8080,出现以下界面说明启动成功了:

图 1

我们新增个 html 页面: hello.html,页面内容为 Hello World,将 html 文件拷贝到 /usr/local/var/www 目录下:

图 2

修改配置:

vim /usr/local/nginx/conf/nginx.conf
在 http {...} 文件中添 include /usr/local/nginx/vhost/*.conf;
http {
   include       mime.types;
   default_type  application/octet-stream;
   ## 加入下面这一行,
   include /usr/local/nginx/vhost/*.conf;

图 3

重启服务:

sudo nginx -s reload

好了,我们成功地更改了欢迎页面~

当然,如果要停止服务执行以下命令先找到 Nginx 进程号,然后 kill 命令:

ps -ef|grep nginx 
sudo kill -QUIT 进程号



二、Nginx的location映射规则

> **语法**:  
> `location [=|~|~*|^~] /uri/ { … }`  
> **说明**:  
> `=` 精确匹配;  
> `^~` :以某个常规字符串开头;  
> `~` :区分大小写的正则匹配;  
> `~*` :不区分大小写的正则匹配;  
> `!` :逻辑取反;  
> `/`:通用匹配;  
> **优先级(从高到低)**:  
> location = /a {…} #精准匹配  
> location ^~ /a {…} #前缀匹配,按最长前缀匹配  
> location ~ /a.* {…} #正则匹配(区分大小写)  
> location ~* /a.* {…} #正则匹配(不区分大小写)  
> location /a {…} #最长前缀匹配,按最长前缀匹配  
> location / {…} #任何没有匹配成功的,都会匹配这里处理

**示例(host:localhost,port:8080为例):**

1.  精准匹配 = "/login",只有访问`http://localhost:8080/login`才能被匹配;  
    location = /login {  
    }
    
2.  以/static/为前缀的路径会被匹配,如:`http://localhost:8080/static/a.html`  
    location ^~ /static/ {  
    }  
    如果有另一个规则:  
    location ^~ /static/css/ {  
    }  
    且访问路径为:`http://localhost:8080/static/css/b.css`  
    则优先匹配规则`^~ /static/css/`,这就是按最长前缀匹配;
    
3.  以`gif|jpg|png|js|css`结尾的路径,区分大小写  
    location ~ .(gif|jpg|png|js|css)$ {  
    }  
    如果访问`http://localhost:8080/static/images/c.PNG`,不会匹配上,需要改成不区分大小写的规则:`~* .(gif|jpg|png|js|css)`
    
4.  访问`http://localhost:8080/api/a` 会被匹配上,同样是按最长前缀匹配;  
    location /api {  
    }
    
5.  当其他规则都不满足时...  
    location /{  
    }
    

二、Nginx的反向代理

> **什么是反向代理?**  
> 反向代理(Reverse Proxy)实际运行方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器,反向代理实际是服务器的代理。

图4.反向代理

Nginx实现反向代理

首先,我们使用node创建一个server,访问地址为: `http://localhost:7001/`,返回内容为:`我被代理了`。

```javascript
var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    response.end('我被代理了');
}).listen(7001);

然后,修改下 ginx.conf 的配置,增加一个 location

图 5.nginx 反向代理配置

我们通过 proxy_pass 将请求路径为 /node 的请求转发到了 http://localhost:7001/ 上。现在我们访问 http://localhost:8080/node 后成功返回我被代理了

三.Nginx http_ssl_module 模块安装

  •   nginx 缺少 http_ssl_module 模块,编译安装的时候带上 --with-http_ssl_module 配置就行了,但是现在的情况是我的 nginx 已经安装过了,怎么添加模块,其实也很简单,往下看: 做个说明:我的 nginx 的安装目录是 /usr/local/nginx 这个目录,我的源码包在 /opt/nginx-1.17.1 目录

  • (1) 切换到源码包:

    cd /opt/nginx-1.17.1  
    
  • (2) 配置信息:

 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  • (3) 配置完成后,运行 make 进行编译,千万不要进行 make install,否则就是覆盖安装。
 make
  • (4) 然后备份原有已经安装好的 nginx
  cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  • (5) 停止 Nginx,正常命令直接 nginx -s stop 就可以
    nginx -s stop
  • (6) 如果关不掉,就直接 Kill 掉进程。ps aux | grep 进程名 查看进程占用的 PID 号。
    ps aux|grep nginx
	
  • (6) 启动 nginx 和查看进程与服务是否正常与开机管理
cd  /usr/local/nginx/sbin
./nginx

#查看nginx进程
ps -ef | grep nginx

#在浏览器访问服务器ip

#设置开机自动启动
vim /lib/systemd/system/nginx.service
#按i编辑 把下面复制进去  按esc建  再按shift+:键 wq  保存退出

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#自己nginx启动的pid文件自己找到文件目录
PIDFile=/usr/local/nginx/logs/nginx.pid
#自己nginx的启动文件 
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#默认
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target


#启动
systemctl start nginx.service

设置开机自启
systemctl enable nginx.service

#提示
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

#停止开机自启动
systemctl disable nginx.service

#查看服务当前状态
systemctl status nginx.service

#重新启动服务
systemctl reload nginx.service

#停止服务
systemctl stop nginx.service
  • (8) 杀掉查出来的 PID 就可以了,kill -9 PID 命令用于终止进程。必须先 kill 掉 root 对应的 PID 才能进行下面的三个 nobody 的 PID。
    kill -9 10922  
    kill -9 28276  
    kill -9 28277  
    kill -9 28278
  • (9) 将刚刚编译好的 nginx 覆盖掉原有的 nginx
    cp ./objs/nginx /usr/local/nginx/sbin/
(10)通过下面的命令查看是否已经加入成功。和重新加载配置
[root@ecs-6090 nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

[root@ecs-6090 nginx]# /usr/local/nginx/sbin/nginx -s reload
  • (11) 如果未启动,则启动 nginx
    nginx

四、Nginx 实践案例

  • 动态资源与静态资源分离(前后端分离)
    现在有很多前后端分离的项目,比如在我们访问 http://localhost:8080/ 时访问网站主页,而访问 http://localhost:8080/api 时又能请求接口,即前端静态资源和后端分离。通过以上的反向代理实现起来很简单:

    图 6. 前后端分离

    root 指向前端静态资源路径。

  • 文件服务器

五、Nginx 或名证书配置实现

  • 域名 server 配置实现 xxx.xxx.com, 修改 nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
在 http { ... } 文件中添  include /usr/local/nginx/conf/vhost/*.conf;   ;
 server {
        listen 8081;
        server_name localhost;
        charset utf-8;
        location /videos {
            root /Users/my_name/Desktop; #文件路径
            autoindex on; #文件列表功能打开
        }
    }
## https 配置如果没有https 证书,下面可以删除 
## ==========================
server {
    listen 443 ssl;
    #配置HTTPS的默认访问端口为443。
    #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
    #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
    server_name  test-im.xxx.cn; #需要将yourdomain.com替换成证书绑定的域名。
    root html;
    index index.html index.htm;
    ssl_certificate cert/xxx.xxx.cn.pem;  #需要将cert-file-name.pem替换成已上传的证书文件的名称。
    ssl_certificate_key cert/xxx.xxx.cn.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的加密套件的类型。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
    ssl_prefer_server_ciphers on;

 location ~/xxx/api/ {
    proxy_pass http://xxx_server;
  }
 
}