Nginx的由来
Nginx(读作engine x)是一个由俄罗斯人Igor Sysoev开发的开源高性能Web服务器软件。起初,Nginx是为Rambler.ru(俄罗斯访问量第二的大型门户网站和搜索引擎)设计的,以解决其高并发访问的需求。Nginx的第一个公开版本0.1.0发布于2004年10月。目前,Nginx通过BSD-like开源软件许可协议发行,可以在多种操作系统中运行,包括UNIX、Linux、macOS、Solaris以及Windows等。
Nginx的作用
Nginx是一个轻量级且功能强大的Web服务器,其主要作用包括:
- 反向代理:Nginx可以作为反向代理服务器,将客户端的请求转发到后端服务器,同时隐藏后端服务器的真实IP地址,提供高可用性。
- 负载均衡:Nginx支持多种负载均衡策略,如轮询、权重分配等,可以将客户端的请求分配到不同的后端服务器上,实现负载均衡,提高系统的整体性能。
- 缓存:Nginx具有强大的缓存功能,可以缓存静态资源,减少对后端服务器的请求,提高访问速度。
- SSL证书配置:Nginx可以配置SSL证书,实现HTTPS访问,提高网站的安全性。
- 防盗链:Nginx可以防止资源被其他网站非法链接和盗用。
- 解决跨域问题:通过配置Nginx,可以方便地解决前后端分离项目中常见的跨域问题。
- 动静资源分离:Nginx可以实现动静资源分离,将静态资源直接返回给客户端,而不需要经过后端服务器处理,提高访问效率。
Nginx的特点
- 高性能:Nginx使用事件驱动模型,可以同时处理大量的并发连接,在高负载和大流量情况下仍然能够保持良好的性能。
- 轻量级:Nginx的代码量非常少,占用内存少,可以在资源受限的系统中运行,并且不容易崩溃。
- 可扩展性:Nginx支持众多的第三方模块,可以根据需要进行自定义开发,实现更多的功能。
- 高度可靠性:Nginx基于稳定、成熟的事件驱动架构开发,能够有效避免由于代码错误或第三方库问题导致的崩溃,保证服务的高可靠性。
- 热部署:Nginx支持在不停止服务的情况下更新配置文件和软件升级,非常方便。
- 高度可定制化:Nginx可以根据需要进行高度定制化,将不需要的模块和功能剔除掉,减少不必要的代码和资源浪费。
Nginx与Apache的对比
Nginx | Apache | |
---|---|---|
由来 | 由Igor Sysoev开发,起初为Rambler.ru设计 | 由Apache软件基金会开发,历史悠久 |
性能 | 高性能,异步非阻塞处理请求 | 性能稳定,但在高并发下可能不如Nginx |
资源占用 | 轻量级,占用内存少 | 相对较重,占用资源较多 |
并发处理能力 | 支持高达50000个并发连接 | 并发处理能力也较强,但不如Nginx |
扩展性 | 支持众多第三方模块,可扩展性强 | 模块化设计,但模块数量可能不如Nginx多 |
可靠性 | 高度可靠,基于稳定的事件驱动架构 | 稳定性良好,但可能出现更多由第三方库引起的问题 |
热部署 | 支持热部署,更新配置和升级方便 | 更新配置和升级通常需要停止服务 |
静态与动态处理 | 对静态资源处理性能高,动态处理稍逊 | 对动态资源处理性能较高,静态处理稍逊 |
社区支持 | 社区活跃,高性能模块出品迅速 | 社区庞大,拥有丰富的模块和解决方案 |
安装:
[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# ls
配置及验证:
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@nginx nginx-1.24.0]# yum install gcc -y
[root@nginx nginx-1.24.0]# yum install pcre-devel.x86_64 -y
[root@nginx nginx-1.24.0]# yum install openssl-devel.x86_64 -y
[root@nginx nginx-1.24.0]# yum install zlib-devel.x86_64 -y
[root@nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
编译
vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin #添加在最后
source ~/.bash_profile
nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with
http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --
with-stream_realip_module
nginx -g "daemon off;"
启动:
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ./nginx
[root@nginx sbin]# ps aux | grep nginx
删除:
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# rm -rf /usr/local/nginx/
[root@nginx nginx-1.24.0]# make clean
平滑升级:
tar zxf nginx-1.26.2.tar.gz
cd nginx-1.26.1/
./configure --with-http_ssl_module --with
http_v2_module --with-http_realip_module --with-http_stub_status_module --with
http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --
with-stream_realip_module
make
ll objs/nginx /usr/local/nginx/sbin/nginx
cd /usr/local/nginx/sbin/
cp nginx nginx.24
\cp -f /root/nginx/nginx-1.26.1/objs/nginx
nginx -t
kill -USR2 48732
ps aux | grep nginx
curl -I localhost
kill -WINCH 48732
curl -I localhost
回退:
cp nginx nginx.26
mv nginx.24 nginx
ps aux | grep nginx
kill -WINCH 52075
curl -I localhost
覆盖:
[root@nginx sbin]# \cp -f /roozt/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/nginx
[root@nginx sbin]# ps ax | grep nginx
[root@nginx sbin]# nginx
启动新版本
[root@nginx sbin]# kill -USR2 57949
回收旧版本
[root@nginx sbin]# kill -WINCH 57949
平滑回滚
[root@nginx sbin]# kill -HUP 57949
回收新版本
[root@nginx sbin]# kill -WINCH 58007
nginx配置
[root@nginx ~]# vim /lib/systemd/system/nginx.service
[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
PIDFile=/usr/local/nginx/logs/nginx.pid
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
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# nginx -s stop
[root@nginx ~]# ps aux | grep nginx
root 42046 0.0 0.1 221664 2304 pts/0 S+ 15:57 0:00 grep --color=auto nginx
[root@nginx ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
新建PC站点
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 100000;
use epoll;
}
…
#gzip on;
include "/usr/local/nginx/conf.d/*.conf";
server {
…
[root@nginx ~]# mkdir -p /usr/local/nginx/conf.d
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
}
[root@nginx ~]# mkdir -p /date/web/html
[root@nginx ~]# echo www.timinglee.org > /data/web/html/index.html
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
location = /test {
root /date/web;
}
[root@nginx ~]# mkdir /date/web/test -p
[root@nginx ~]# echo test page > /date/web/test/index.html
精确匹配
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# mkdir /date/web/test -p
[root@nginx ~]# echo test page > /date/web/test/index.html
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
location = /test {
root /date/web2;
}
优先级
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
location = /test {
root /date/web2;
}
location /test {
root /date/web1;
}
location ^~ /t {
root /date/web3;
}
location ~ .html$ {
root /date/web4;
}
location ~* .HTML$ {
root /date/web5;
}
}
[root@nginx ~]#
[root@nginx ~]# mkdir -p /date/web{1..5}/test
[root@nginx ~]# echo web1 > /date/web1/test/index.html
[root@nginx ~]# echo web2 > /date/web2/test/index.html
[root@nginx ~]# echo web3 > /date/web3/test/index.html
[root@nginx ~]# echo web4 > /date/web4/test/index.html
[root@nginx ~]# echo web5 > /date/web5/test/index.html
nginx用户认证
[root@nginx ~]# yum install httpd-tools
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd lee #-c会覆盖原来的
New password:
Re-type new password:
Adding password for user lee
[root@nginx ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$HhAqh9XC$3X1Hi5gYYuHONKedAp7lb1
lee:$apr1$FC4uoJ1U$1B47c8l/LiXgMCXJvhs2D1
[root@nginx ~]#
[root@nginx ~]# mkdir /date/web/lee
[root@nginx ~]# echo lee > /date/web/lee/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
location /lee {
root /date/web;
}
}
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
location /lee {
root /date/web;
auth_basic "login password !!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
自定义错误页面
[root@nginx ~]# mkdir -p /date/web/errorpage
[root@nginx ~]# echo error page > /date/web/errorpage/40x.html
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
error_page 404 /40x.html;
location /lee {
root /date/web;
auth_basic "login password !!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /date/web/errorpage;
}
}
[root@nginx ~]# nginx -s relod
自定义错误日志
[root@nginx ~]# mkdir /var/log/timinglee.org
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
error_log /var/log/timinglee.org/error.log;
[root@nginx ~]# nginx -s reload
检测日志是否存在
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.timinglee.org;
root /date/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/timinglee.org/error.log;
access_log /var/log/timinglee.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /lee {
root /date/web;
auth_basic "login password !!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /date/web/errorpage;
}
}
[root@nginx ~]# mkdir /date/web/html/error
[root@nginx ~]# echo error default > /date/web/html/error/default.html
[root@nginx conf.d]# vim /etc/hosts
192.168.86.100 nginx.org www.timinglee.org
[root@nginx conf.d]# curl www.timinglee.org
error default
长连接
[root@nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 65;
keepalive_requests 2;
测试工具
[root@nginx conf.d]# yum install telnet -y
[root@nginx ~]# telnet www.timinglee.org 80
Trying 192.168.86.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
#keepalive_timeout 0;
keepalive_timeout 5;
keepalive_requests 500;
[root@nginx ~]# telnet www.timinglee.org 80
Trying 192.168.86.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
作为下载服务器配置
[root@nginx ~]# mkdir /date/web/download
[root@nginx ~]# dd if=/dev/zero of=/date/web/download/leefile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0459089 s, 2.3 GB/s
[root@nginx ~]# nginx -s reload
location /download {
root /date/web;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate 1024k;
}
ngx_http_rewrite_module 模块指令
if指令
if (条件匹配) {
action
}
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false。
set指令
指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key
另外set定义格式为set $key value,value可以是text, variables和两者的组合。
break指令
用于中断当前相同作用域(location)中的其他Nginx配置
与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效
位于后面的 ngx_http_rewrite_module 模块中指令就不再执行
Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配
该指令可以在server块和locationif块中使用
注意: 如果break指令在location块中后续指令还会继续执行,只是不执行 ngx_http_rewrite_module模块的指令,其它指令还会执行
return指令
return用于完成对请求的处理,并直接向客户端返回响应状态码,比如:可以指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配置都将不被执行,return可以在server、if 和 location块进行配置
语法格式
rewrite指令
通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配
rewrite主要是针对用户请求的URL或者是URI做具体处理
rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI
注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制
如果替换后的URL是以http://或https://开头,则替换结果会直接以重定向返回给客户端, 即永久重定向301
语法格式
nginx防盗链
防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有:
none:
blocked:
server_names:
arbitrary_string:
www.timinglee.*
regular expression:
实现盗链
在一个web 站点盗链另一个站点的资源信息,比如:图片、视频等
#盗链网页
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://www.timinglee.org/images/lee.png" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://www.timinglee.org>薇尔莉特</a>伊芙加登</p>
</body>
</html>
实现防盗链
基于访问安全考虑,nginx支持通过ngx_http_referer_module模块,检查访问请求的referer信息是否有效实现防盗链功能
server {
listen 80;
listen 443 ssl;
server_name www.timinglee.org;
root /data/web/html;
index index.html;
ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;
ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
location / {
if ( $scheme = http ){
rewrite /(.*) https://$host/$1 redirect;
}
if ( !-e $request_filename ){
rewrite /(.*) https://$host/index.html redirect;
}
}
#防盗链
location /images {
valid_referers none blocked server_names *.timinglee.org ~/.baidu/.;
if ( $invalid_referer ){
rewrite ^/ http://www.timinglee.org/daolian.png;
}
}
#盗链网页
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://www.timinglee.org/images/lee.png" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://www.timinglee.org>薇尔莉特</a>伊芙加登</p>
</body>
</html>
安装php
yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
make && make install
php测试页面
server {
listen 80;
server_name www.kkk.org;
root /data/web/php;
index index.html;
location /leo {
root /data/web;
auth_basic "password";
auth_basic_user_file "usr/local/nginx/.htpasswd";
}
}
mkdir /data/web/php
touch /data/web/php/index.php
vim /data/web/php/index.php
<?php
phpinfo();
?>