安装教程
Nginx安装
步骤1:app开发定制安装编译工具和库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
- 1
步骤2:安装PCRE
1、通过wget下载PCRE
app开发定制先创建一个nginxapp开发定制的存放目录将pcreapp开发定制下载到创建的目录下,app开发定制我创建的目录如下:
cd /gnhmkdir nginxwget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
- 1
- 2
- 3
2、app开发定制解压安装包
tar -zxvf pcre-8.35.tar.gz
- 1
3、app开发定制进入安装包目录
cd pcre-8.35
- 1
4、编译安装
./configure make && make install
- 1
- 2
5、查看pcre版本
pcre-config --version
- 1
步骤3:开始安装nginx
1、切换到要保存安装包的目录下通过wget下载安装包
cd /gnh/nginxwget http://nginx.org/download/nginx-1.22.0.tar.gz
- 1
- 2
2、解压安装包
tar -zxvf nginx-1.22.0.tar.gz
- 1
3、进入安装包目录
cd nginx-1.22.0.tar.gz
- 1
4、编译安装
4.1如果需要配置gzip的gzip_static配置,需要安装–with-http_gzip_static_module模块
./configure --prefix=/gnh/nginx/nginx-1.22.0 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/gnh/nginx/pcre-8.35 --with-http_gzip_static_modulemakemake install
- 1
- 2
- 3
4.2 如果不需要打开gzip_static可以不用安装–with-http_gzip_static_module模块
./configure --prefix=/gnh/nginx/nginx-1.22.0 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/gnh/nginx/pcre-8.35makemake install
- 1
- 2
- 3
5、查看nginx版本
/gnh/nginx/sbin/nginx -v
- 1
到此,nginx安装完成啦。
6、启动nginx
启动时可能会提示logs目录不存在,可以自己在安装nginx目录下创建一个logs目录,然后进行授权,之后再进行启动就好啦。启动目录不是在sbin下的nginx哦,要在objs下的nginx进行启动哦。
mkdir logschmod -R 777 logs./objs/nginx
- 1
- 2
- 3
7、查看启动时候成功
在浏览器上进行访问
http://localhost:8080
这样就可以看到nginx的页面啦!这样就说明启动成功啦!
注意:如果访问不到,看下是否防火墙没开,没开的开放下端口,开放端口命令:
firewall-cmd --zone=public --add-port=端口号/tcp --permanentfirewall-cmd --reload
- 1
- 2
额外附带一些常用linux命令:
查看防火墙开放端口命令:
netstat -nplt 或 firewall-cmd --list-ports
关闭防火墙开放端口命令删除后记得reload:
firewall-cmd --zone=public --remove-port=端口号/tcp --permanent
firewall-cmd --reload
查看防火墙状态:
firewall-cmd --state
步骤4:配置gzip 文件压缩(如有需要)
#开启gzip功能gzip on;#开启gzip静态压缩功能gzip_static on;#最小从1k开始压缩,小于1k不压缩#gzip_min_length 1k;##gzip缓存大小gzip_buffers 4 16k;#gzip http版本gzip_http_version 1.1;#gzip压缩级别 1-10gzip_comp_level 4;#压缩类型gzip_types text/plain application/javascript application/x-javascript text/css text/javascript image/jpeg image/gif image/pnggzip_vary on;#gzip_proxied expired no-cache no-store private auth;#IE6 不兼容,表示IE6时禁用gzipgzip_disable "MSIE [1-6]\.";
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
gzip_proxied 配置说明:
gzip_proxied指令:该指令设置是否对服务端返回的结果进行Gzip压缩。
off - 关闭Nginx服务器对后台服务器返回结果的Gzip压缩
expired - 启用压缩,如果header头中包含"Expires" 头信息
no-cache - 启用压缩,如果header头中包含 “Cache-Control:no-cache"头信息
no-store - 启用压缩,如果header头中包含 “Cache-Control:no-store” 头信息
private启用压缩,如果header头中包含 “Cache-Control:private” 头信息
no_last_modified - 启用压缩,如果header头中不包含 “Last-Modified” 头信息
no_etag - 启用压缩 ,如果header头中不包含"ETag” 头信息
auth - 启用压缩 , 如果header头中包含 “Authorization” 头信息
any -无条件启用压缩
步骤5:访问配置
1、前端地址访问配置(如vue打包后的dist文件夹访问)
location / { root /gnh/web/dist; index index.html; try_files $uri $uri/ /index.html; }
- 1
- 2
- 3
- 4
- 5
location / : 表示在浏览器范围的时候直接ip + port 就会转发到你的dist前端页面没有前缀
root : 填自己的dist所在的目录下
2、后台接口访问配置
location /api{ proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
location /api : 表示当访问的接口路径以 /api开头时,跳转到proxy_pass代理的后台地址
proxy_pass : 需要跳转到的后台地址
3、location 后面有 / 和无 / 的区别
- 情况1:location和proxy_pass都带/,则真实地址不带location匹配目录
location /api/ { proxy_pass http://127.0.0.1:8080/;}
- 1
- 2
- 3
访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/upload
- 1
- 情况2:location不带/,proxy_pass带/,则真实地址会带/
location /api { proxy_pass http://127.0.0.1:8080/; }
- 1
- 2
- 3
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080//upload
- 1
- 情况3:location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/
location /api/ { proxy_pass http://127.0.0.1:8080; }
- 1
- 2
- 3
访问地址: www.test.com/api/upload–>http://127.0.0.1:8080/api/upload
- 情况4:location和proxy_pass都不带/,则真实地址会带location匹配目录/api/
location /api { proxy_pass http://127.0.0.1:8080; }
- 1
- 2
- 3
访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload
- 1
总结:
1.proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录
2.proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部
6、Nginx常用命令
校验配置nginx.config配置是否正确命令:
/gnh/nginx/objs/nginx -t 或者有些是在sbin下面执行 /gnh/nginx/sbin/nginx -t
重新加载配置文件命令:
/gnh/nginx/objs/nginx -reload 或者有些在sbin下执行的 /gnh/nginx/sbin/nginx -s reload
重启nginx:
/gnh/nginx/objs/nginx -s reopen 或者有些是在sbin下执行的 /gnh/nginx/sbin/nginx -s reopen
停止nginx:
/gnh/nginx/objs/nginx -s stop 或者有些是在sbin下执行的 /gnh/nginx/sbin/nginx -s stop
7、Nginx卸载
1.检查nginx服务是否运行,如果正在运行则关闭服务。
ps -ef | grep nginx
/gnh/nginx/objs/nginx -s stop
2.查找并删除nginx相关文件。
whereis nginx
find / -name nginx
rm -rf /usr/local/nginx
3.卸载nginx依赖
yum remove nginx