Nginx详细入门教程
一、Nginx部署安装教程
1.Nginx简介
Nginx是一个高性能的HTTP和反向代理Web服务器,同时也提供了IMAP/POP3/SMTP服务。
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占用内存少,并发能力强,事实上Nginx的并发能力在同等类型的网页服务器中表现较好。
2.下载
nginx官网: http://nginx.org/
可自行选择版本并下载
wget http://nginx.org/download/nginx-1.10.1.tar.gz
3.安装Nginx依赖
yum install -y gcc-gcc++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@zxy_master nginx-1.10.1]# yum install -y gcc-gcc++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
......
4.解压
[root@zxy_master software]# tar -zxvf nginx-1.10.1.tar.gz -C /zxy/apps/
nginx-1.10.1/
nginx-1.10.1/auto/
nginx-1.10.1/conf/
nginx-1.10.1/contrib/
nginx-1.10.1/src/
......
5.配置
./configure --prefix=/usr/local/nginx
[root@zxy_master nginx-1.10.1]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-1160.45.1.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
6.编译和安装
make & make install -j 4
[root@zxy_master nginx-1.10.1]# make & make install -j 4
[1] 22058
make -f objs/Makefile
make -f objs/Makefile install
make[1]: Entering directory `/zxy/apps/nginx-1.10.1'
make[1]: Entering directory `/zxy/apps/nginx-1.10.1'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/sr
7.创建软连接(可选)
刚才配置路径在/usr/local/nginx
,通过创建软连接,方便查看
[root@zxy_master nginx-1.10.1]# ln -s /usr/local/nginx/
8.启动nginx
[root@zxy_master sbin]# ./nginx
[root@zxy_master sbin]# ps -ef | grep nginx
root 24318 1 0 13:56 ? 00:00:00 nginx: master process ./nginx
nobody 24319 24318 0 13:56 ? 00:00:00 nginx: worker process
root 24365 2006 0 13:56 pts/0 00:00:00 grep --color=auto nginx
9.关闭nginx
[root@zxy_master sbin]# ./nginx -s quit
[root@zxy_master sbin]# ps -ef | grep nginx
root 24876 2006 0 13:57 pts/0 00:00:00 grep --color=auto nginx
二、前后端分离代码,前端刷新404,调整nginx.conf实现URL重写
1.初始nginx.conf配置
location / {
root html;
index index.html index.htm;
}
2.出现问题(刷新即404)
2.关闭nginx
## 关闭
D:\nginx-1.20.2>nginx.exe -s quit
3.修改nginx.conf,添加URL重定向
location / {
root html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
}
3.重启nginx
## 开启
D:\nginx-1.20.2>start nginx
## 重加载
D:\nginx-1.20.2>nginx.exe -s reload
4.尝试刷新
三、安装nginx 启动后报错
1.nginx error.log
[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client:
2.解决方案:
我的conf server是这样配置的:
# curl https://ssl-config.mozilla.org/ffdhe2048.txt > /etc/nginx/ssl/dhparam.pem
# ssl_dhparam ssl/dhparam.pem;
location / {
client_max_body_size 20M;
proxy_pass http://xxx.xxx.xxx.xxx:8091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
3.注意这里:
location / {
client_max_body_size 20M;
# 我的问题就是这里 原来是:proxy_pass http://localhost:9000;
# 后来改成:proxy_pass http://127.0.0.1:9000; 也不行!
# 改成解决:proxy_pass http://xxx.xxx.xxx:9000; ok了!(xxx.xxx.xxx是你服务器公网IP 如:139.172.99.21:9000)
proxy_pass http://xxx.xxx.xxx.xxx:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
四、Nginx的卸载
1.停止Nginx服务
systemctl stop nginx.service
2.删除Nginx的自启动
systemctl disable nginx.service
3.从源头删除Nginx
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
4.再使用yum清理
yum remove nginx