nginx学习

一、nginx常用版本

Nginx开源版:
http://nginx.org/
nginx plus商业版本(好像功能支持更多)
https://www.nginx.com/
openresty (免费,用的也是这个)
https://openresty.org/cn/
Tengine
https://tengine.taobao.org/
最后学习openresty,并且公司用的也是这个openresty。

二、安装nginx(openresty)

linux安装nginx

注意:我的linux发行版本是
CentOS Linux release 7.6.1810 (Core)

1、安装依赖

OpenResty依赖库有:perl 5.6.1+, libreadline, libpcre, libssl。不然你会在 运行 ./configure 就会编译错误

#在CentOS上安装如下
yum -y install readline-devel pcre-devel openssl-devel gcc perl curl

2、下载源码

1、随便找个目录将openresty-1.11.2.5.tar.gz 压缩包下载下来。
2、然后使用tar解压缩
3、进入到 openresty-1.11.2.5 中 开始进行编译安装 openresty

wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
tar zxvf openresty-1.11.2.5.tar.gz
cd openresty-1.11.2.5

3、编译安装

configure配置脚本 它需要一些依赖就是上面那些,不然你尝试运行就会报错
–prefix 安装在哪个目录下 一般都会装在/usr/local/openresty
1、注意我安装的是openresty,如果安装nginx的话 貌似下载的依赖不一样。
2、./configure 开始编译
3、参数解释
–prefix=/usr/local/openresty:指定OpenResty的安装目录为/usr/local/openresty。
–with-luajit:启用LuaJIT,这是一个高性能的Lua解释器,可以提高OpenResty的性能。
–without-http_redis2_module:禁用Redis2模块,这个模块用于与Redis数据库进行交互。
–with-http_iconv_module:启用iconv模块,这个模块用于字符集转换。
gmake:这个命令是用来编译OpenResty的源代码,将源代码编译成可执行文件。
gmake install:这个命令是用来安装OpenResty,将编译好的可执行文件和相关的库文件复制到指定的安装目录中。

./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module
gmake
gmake install

4、启动nginx服务

1、手动启动方式

首先安装完成后,没有系统服务去管理或者脚本,只有自己手动去启动。
/usr/local/openresty/nginx/sbin/nginx
验证下是否启动成功。通过curl cip.cc查询外网ip。然后访问浏览器这个ip地址。如果服务启动,访问没反应,应该是防火墙问题,关掉防火墙即可,我直接用的公司服务器,没遇到过这问题。
关闭防火墙systemctl stop firewalld.service
到这就完成了,访问ip有响应代表成功。

#启动nginx
./nginx
#快速停止
./nginx -s stop
#优雅的关闭,在退出前完成已经接受的链接请求
./nginx -s quit
#重新加载配置
./nginx -s reload 
2、安装成系统服务

接下来加入到 systemctl 中 用来管理服务
1、创建一个新的systemd服务文件nginx.service:
vim /usr/lib/systemd/system/nginx.service
2、内容如下

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3、保存并关闭nginx.service文件。
4、
#启用Nginx服务:
systemctl enable /usr/lib/systemd/system/nginx.service
#启动Nginx服务:
systemctl start nginx.service
此时就可以用 systemctl 来启用和关闭nginx了。

docker安装nginx

#1、下载nginx镜像 默认下载最新的nginx
docker pull nginx
#2、将nginx中的文件拷贝到本机,后面用于数据卷 docker cp 用于容器与主机之间的数据拷贝。
docker cp nginx:/etc/nginx/nginx.conf /Users/xxx/docker/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /Users/xxx/docker/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /Users/xxx/docker/nginx/html
#3、启动一个nginx镜像容器,映射端口这里映射了两个
docker run \
-p 8000:80 \
-p 8001:81 \
--name nginx \
-v /Users/xxx/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/xxx/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /Users/xxx/docker/nginx/log:/var/log/nginx \
-v /Users/xxx/docker/nginx/html:/usr/share/nginx/html \
-d nginx:latest

1、docker下使用nginx

配置都直接在本机写即可。
conf.d下的配置文件
vod.conf

server { # server 代表一个主机 虚拟主机 也叫 vhost ,每个主机 都可以有一个根目录 互相不干扰
    listen       80; # 监听端口
    listen  [::]:80; 
    server_name  localhost; # 当前主机的主机名 可以配置域名 或者主机名

    location / { # 域名后面跟的子路径  http://test.com/test/index.html url  /test/index.html uri 
        root   /www/vod; # 当前匹配上location 之后 用户请求的从该目录去找它相应的网页
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html; # 服务端返回的错误码 我们会转向到 后面的页面 即访问  http://test.com/50x.html 
    location = /50x.html { # 然后访问了 http://test.com/50x.html  又匹配到了 这里 访问 /usr/share/nginx/html 目录下 
        root   /usr/share/nginx/html;
    }
}

www.conf

server { # server 代表一个主机 虚拟主机 也叫 vhost ,每个主机 都可以有一个根目录 互相不干扰
    listen       81; # 监听端口
    server_name  localhost; # 当前主机的主机名 可以配置域名 或者主机名

    location / { # 域名后面跟的子路径  http://test.com/test/index.html url  /test/index.html uri 
        root   /www/www; # 当前匹配上location 之后 用户请求的从该目录去找它相应的网页
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html; # 服务端返回的错误码 我们会转向到 后面的页面 即访问  http://test.com/50x.html 
    location = /50x.html { # 然后访问了 http://test.com/50x.html  又匹配到了 这里 访问 /usr/share/nginx/html 目录下 
        root   /usr/share/nginx/html;
    }
}

然后配置html文件
mkdir -p www/www/index.html
echo ‘this is www site’ > index.html
mkdir -p www/vod/index.html
echo ‘this is vod site’ > index.html
访问
http://127.0.0.1:8000/
显示
this is vod site
访问
http://127.0.0.1:8001/
显示
this is www site

三、nginx使用

openresty nginx目录介绍

client_body_temp
conf
fastcgi_temp
html
logs
proxy_temp
sbin
scgi_temp
uwsgi_temp

sbin :下面就一个文件 ,即 nginx 的主程序
conf:配置文件,其中最主要的是nginx 的主配置文件nginx.conf,这个里面可能会引用其他配置,比如mime.types 等
html:默认情况下网页和静态资源。比如 index.html
logs:记录日志,access.log 访问日志 用户、时间、等信息。error.log 错误日志 nginx.pd 记录nginx的主进程号

页面开始
我们尝试访问curl 'http://127.0.0.1'
出现了

<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="https://openresty.org/">openresty.org</a>.<br/></p>

<p><em>Thank you for flying OpenResty.</em></p>
</body>
</html>

实际就是nginx的配置开始了转发请求映射到某个文件。

配置文件

nginx.conf 结构

main 全局配置
  event 配置工作模式以及连接数
  http http模块相关配置
  server 虚拟主机配置,可以有多个
   location 路由规则,表达式
   upstream 集群,内网服务器

配置文件 nginx.conf

# 就是worker的进程用户 通过 ps -ef | grep ngixn 查看 worker的进程用户就是它配置的 默认不写就是 nobody 
user nobody
#工作线程的个数 auto 自适应 
worker_processes auto;
error_log /data/logs/error.log;
#error_log /data/logs/ngerr.log crit;
#error_log /data/logs/ngerr.log info;
#error_log /data/logs/ngerr.log notice;
# 错误日志配置 他们也有级别的 1 debug 2 info 3 notice 4 warn 5 crit 从1-5 级别一次递增。

pid /var/run/nginx.pid;
# pid 进程号位置 

#events 事件处理 epoll:实现高并发下大量请求。linux下是 epoll模型 其他操作系统需要改
events {
  #使用epoll 
  use epoll;
  # 每个worker允许连接的客户端最大连接数
  worker_connections 51200;
}

http{
  include mime.types;#请求的设置 include 引入一个配置文件 mime.types 设置的
  default_type application/octet-stream;# 设置了默认类型
#  log_format main   '$remote_addr - stable=$stable $tracecode $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" '
#                      '"$http_user_agent" "H:$http_host,RE:$http_x_real_ip,F:$http_x_forwarded_for" '
#                      '"$http_user_agent" '
#                      '$request_length $request_time $upstream_response_time $request_body';
以上是对日志的格式做配置
access_log logs/access.log main;设置日志访问路径
sendfile on;# 提升文件传输
tcp_nopush on;# 等到一定的tcp包才传送 提高传输效率 这个必须是 sendfile on这个开启才有效

}

1、location中 proxy_pass 、root、index

location 中的 proxy_pass 和 root、index 只能存在一个。
proxy_pass 就是,访问 80 端口就会 被代理到 http://www.baidu.com

location / {
	   listen       80;
      proxy_pass http://www.baidu.com ; 
       #root   /www/vod;  
       #index  index.html index.htm;
    }

2、upstream 实现负载均衡

思路:使用docker 开启三个端口 前两个端口 是访问后代理到页面的,第三个端口 负责来实现负载均衡的。没必要开三个虚拟机 搞,用docker 开一个nginx 多端口 同样能实现。另外用了数据卷,直接本机就能映射到里面的配置文件。不然每次删除nginx重新分配多个端口时 还要重新写配置,比较麻烦。

1、使用docker开三个端口 80、81 是访问内容的。82 是实现负载均衡的

docker run \
-p 8000:80 \
-p 8001:81 \
-p 8002:82 \
--name nginx \
-v /Users/xxx/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/xxx/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /Users/xxx/docker/nginx/log:/var/log/nginx \
-v /Users/xxx/docker/nginx/html:/usr/share/nginx/html \
-d nginx:latest

2、配置文件如下:
/Users/xxx/docker/nginx/conf/conf.d/balance.conf

upstream vod_www {
 server 127.0.0.1:80 ; 
 server 127.0.0.1:81 ; 
}

server {
 listen 82;
 server_name 127.0.0.1;
 location / {
   proxy_pass http://vod_www;
 }
}

/Users/xxx/docker/nginx/conf/conf.d/vod.conf

server { 
    listen       80; # 监听端口
    listen  [::]:80; 
    server_name  localhost; 
    location / {
      #proxy_pass http://vod ; 
      #proxy_pass http://www.baidu.com ; 
      #root   /www/vod;  
      root /usr/share/nginx/html/vod;
      index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html; 
    location = /50x.html { 
        root   /usr/share/nginx/html;
    }
}

/Users/xxx/docker/nginx/conf/conf.d/www.conf

server { 
    listen       81; 
    server_name  localhost; 

    location / { 
        #root   /www/www; 
        root /usr/share/nginx/html/www;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html; 
    location = /50x.html { 
        root   /usr/share/nginx/html;
    }
}

3、html文件
html 的index文件
/Users/xxx/docker/nginx/html/vod/index.html
this is vod site~
/Users/liuyuanyuan/docker/nginx/html/www/index.html
this is www site

访问 http://127.0.0.1:8002/
会轮询的 127.0.0.1:80 和 127.0.0.1:81 端口 进而显示
this is vod site~ 或 this is www site
到此实现完毕~。

3、upstream weight 权重

默认的负载均衡的策略是轮询。
weight参数是主要配置负载均衡策略使用的, 称为负载均衡的 “权重”策略
balance.conf
配置了权重,80端口接受请求3次,81端口接受请求1次

upstream vod_www {
 server 127.0.0.1:80 weight=3 ; 
 server 127.0.0.1:81 weight=1 ; 
}

server {
 listen 82;
 server_name 127.0.0.1;
 location / {
   proxy_pass http://vod_www;
 }
}

4、upstream down 标记不可用

down用于标记服务节点不可用:
此时只会有81端口接受请求。

upstream vod_www {
 #server 127.0.0.1:80 weight=3 ; 
 server 127.0.0.1:80 down ; 
 server 127.0.0.1:81 weight=1 ; 
}

server {
 listen 82;
 server_name 127.0.0.1;
 location / {
   proxy_pass http://vod_www;
 }
}

5、upstream指令参数 backup

backup表示当前服务器节点是备用机,只有在其他的服务器都宕机以后,自己才会加入到集群中,被用户访问到:

upstream vod_www {
 server 127.0.0.1:80 backup ; 
 server 127.0.0.1:81 weight=1 ; 
}

server {
 listen 82;
 server_name 127.0.0.1;
 location / {
   proxy_pass http://vod_www;
 }
}

6、防盗链配置

valid_referers 127.0.0.1 检测来源的网址,注意这不是ip

/Users/xxx/docker/nginx/conf/conf.d/balance.conf

upstream vod_www {
 server 127.0.0.1:80 down ; 
 server 127.0.0.1:81 weight=1 ; 
}

server {
 listen 82;
 server_name 127.0.0.1;
 location / {
   proxy_pass http://vod_www;
 }
 # 以png等结尾的文件会被检测 通过则会走下面 root逻辑
 location ~.*\.(png|jpg|gif)$ {
  valid_referers 127.0.0.1;
  if ($invalid_referer){
   return 401;
  }
  root /usr/share/nginx/html;
  index index.html;
 }
 location /index.html { 
  root /usr/share/nginx/html;
 }
}

1、直接访问 http://127.0.0.1:8002/img/defaultavatar.png
会发生
401 Authorization Required
nginx/1.21.5
因为此时没有 referer 所以访问不了。当然你可以配置 none
这样

location ~.*\.(png|jpg|gif)$ {
  valid_referers none 127.0.0.1;
  if ($invalid_referer){
   return 401;
  }
  root /usr/share/nginx/html;
  index index.html;
 }

这样他检测的时候发现你请求中没有 referer 他也会通过检测。
2、访问 http://127.0.0.1:8002/index.html
index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<img src="http://127.0.0.1:8002/img/defaultavatar.png">
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

会发现图片访问正常 ,因为请求头中有referer
Referer:
http://127.0.0.1:8002/index.html
所以检测通过了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值