《Nginx系列》Nginx详细入门教程

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
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
nginx是一个高性能的开源Web服务器软件,它可以作为反向代理服务器、负载均衡器和HTTP缓存服务器等多种用途。对于初学者,可以通过阅读《nginx 快速入门》这本PDF来学习nginx的基本知识和使用方法。 《nginx 快速入门》这本PDF提供了全面而详细的关于nginx的介绍和使用指南。首先,它简要介绍了nginx的历史、特点和优势,帮助读者对nginx有一个整体的了解。然后,它详细介绍了nginx的安装和配置过程,包括如何在不同操作系统上安装nginx、如何配置nginx的基本选项和参数等。 除了安装和配置,这本PDF还介绍了nginx的核心功能和常用模块的使用方法。例如,它详细介绍了如何配置nginx作为反向代理服务器,将客户端的请求转发到后端的应用服务器;如何配置nginx作为负载均衡器,实现请求的分发和负载均衡;以及如何配置nginx作为HTTP缓存服务器,提高Web应用的性能等。 此外,这本PDF还介绍了nginx的安全性和高可用性相关的内容,例如如何配置SSL证书进行HTTPS加密传输、如何配置基于HTTP Basic Authentication的访问控制、如何配置nginx实现故障转移和负载均衡等。 总之,《nginx 快速入门》这本PDF是一本适合初学者快速入门nginx的指南。通过阅读这本PDF,读者可以了解nginx的基本概念和使用方法,并能够配置和管理一个基本的nginx服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DATA数据猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值