nginx的使用实战

一定要学会看nginx的日志文件,是解决问题的关键要素

前端仔也需要懂的nginx内容:https://juejin.cn/post/7007346707767754765#heading-32
菜鸟教程:https://www.runoob.com/w3cnote/nginx-setup-intro.html

官方文档地址:http://nginx.org/en/docs/

一、linux 安装nginx

1、下载nginx nginx下载地址
2、复制到linux 系统
3、解压并进入nginx目录

tar -zxvf nginx-1.19.9.tar.gz
cd nginx-1.19.9

4、使用nginx 默认配置 (解压后没有sbin目录,是因为没有编译安装)

./configure

5、编译安装

make
make install

二、可能出现的问题的解决方案

6.1 执行make出现:make: *** No rule to make target `build’
执行make出现问题解决方案
(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.

解决方案:需要安装pcre包
https://ftp.pcre.org/pub/pcre/
执行如下操作

tar zxvf pcre-8.12.tar.gz
cd pcre-8.12
./configure
make
make install

(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

解决方案:下载地址http://www.zlib.net/

执行命令

tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

出现错误

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

解决方案:杀掉所有的进程

killall -9 nginx

出现错误

Nginx配置https代理启动报错 nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/nginx.conf:46

这个主要原生是因为nginx没有安装ssl模块
解决方案:如果无法停止nginx 则killall -9 nginx 杀掉进程

[root@120 ~]# cd /usr/local/src/nginx-1.6.1
[root@120 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx
[root@120 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@120 nginx-1.16.1]# make

[root@120 nginx-1.16.1]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@120 nginx-1.16.1]# cp ./objs/nginx /usr/local/nginx/sbin/
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y

如果提示:cp: 无法创建普通文件"/usr/local/nginx/sbin/nginx": 文本文件忙
先把nginx停止
[root@120 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -s quit
然后启动
[root@120 nginx-1.16.1]# /usr/local/nginx/sbin/nginx

三、nginx 常用的命令

Linux中,nginx命令总结

进入 usr/local/nginx/sbin 目录
1、测试配置信息是否有错误

./nginx -t

2、启动服务

./nginx 
start nginx

3、停止服务

./nginx -s stop

4、重启服务

./nginx -s reload

四、常用配置

页面访问有问题,记得看日志文件(logs)

 rewrite ^/api/(.*) /$1 break;

这 Nginx 的 rewrite 指令的含义是将 /api/ 路径的请求转发为去掉 /api/ 部分的路径
举个例子,如果有一个请求是 http://example.com/api/some/path,经过这个 rewrite 规则处理后,会被重写为 http://example.com/some/path。

在这里插入图片描述

 location  /msb/mengsuban/static/ {
	rewrite ^/msb/mengsuban/static/(.*) /$1 break;
	root   html/msb/mengsuban/msb/mengsuban/static;

}

五、其他

1、nginx 启动日志中出现这个signal process started
解决办法

ps -ef | grep nginx
pkill -9 nginx //杀掉nginx
./nginx //重启服务

2、端口被占用

 lsof -i:端口号 //查看某一端口的占用情况
 kill -9 pid //杀死进程
 

六、nginx rewrite 用法详解

Nginx——Rewrite用法详解

定义:

rewrite功能就是使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。

server {
        listen 80;

        server_name www.mfc.com;

        rewrite  ^/api/(.*)  http://www.test.com/$1 permanent;

}


访问地址:www.mfc.com/api/login
URL就会被重写为:www.test.com/login

七、配置多个路径

注意配置文件打包路径 最好是使用相对路径或者带有名称的绝对路径

例如:
1、绝对路径:
在这里插入图片描述
2、相对路径:
可以支持 在mainfest.json中设置h5节点的publicPath为:'./'打包后即为相对路径 但是你会发现一个问题就是有tabbar的情况下你的图标找不到 这是你需要手动改打包后的index.xxxxx.js文件 把这个e.uniConfig.router={mode:“hash”,base:“/”}改成e.uniConfig.router={mode:“hash”,base:“./”}即可

在这里插入图片描述

在这里插入图片描述
*

八、nginx root 和 alias 配置区别

在这里插入图片描述

九、代理https协议的出现问题

nginx使用ssl模块配置支持HTTPS访问
在这里插入图片描述
步骤1:切换到源码包

cd /home/cqct/nginx

步骤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

步骤6:将刚刚编译好的nginx覆盖掉原有的nginx

cp ./objs/nginx /usr/local/nginx/sbin/

步骤7:启动nginx

./nginx

十、路由为history的配置

	location /qssview {
            root html;
            index  index.html index.htm;
            try_files $uri $uri/ /qssview/index.html last;
        } 
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值