Nginx配置

nginx配置文件

#user  nginx;		#通过ps -ef |grep nginx 查看到的用户就是这里配置的
worker_processes  1;		#子进程数量,一般调整为cpu的核数或者倍数

#error_log  logs/error.log;		#错误日志定义						
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;		

#pid        logs/nginx.pid;		#进程pid 存储文件


events {		#日志
worker_connections  1024;		#每个子进程的连接数,nginx的当前并发量=worker_processes * worker_connections
}


http {		#http协议段
include       mime.types;		#引入  文件扩展名和与文件类型映射表
default_type  application/octet-stream;		 #默认文件类型

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '		 #访问日志access.log的格式
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log     main;		
#开启日志    #访问日志存储路径   #调用格式与上一段的定义一致

sendfile        on;		 #linux内核  提供加快文件读写的机制
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;		 #长连接超时时间,单位为s,当用户端和服务器的连接时间超过65s,服务器的连接就会回收

#gzip  on;		#gzip压缩

server {		
listen       80;		#监听端口
server_name  localhost;		#域名,可以有多个,用空格分隔

#charset koi8-r;		#默认编码

#access_log  logs/host.access.log  main;		

location / {		#用来匹配url
root   html;		#默认访问网站的路径
index  index.html index.htm;	#默认访问页面,从前往后的顺序查找
autoindex on;  #加在index index.html后面一行,找不到文件路径时就会以列表显示
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
	 如果要找具体的后端代码,在/code/去找
   root /code;
   # 会把请求转发给后端的php-fpm进行加工
   fastcgi_pass 127.0.0.1:9000;
   # 默认的首页文件名字是index.php
   fastcgi_index index.php;
   # 保留用户想请求的脚本名,以及具体的uri信息的
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		 # 导入一个文件,能够让fastcgi识别http的这些内置变量
		 # 在nginx的默认路径下,/etc/nginx/fastcgi_params
   include fastcgi_params;
}


# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#		 allow 192.168.2.10
#    deny  all;禁止访问ip   #可以先allow几个ip,再deny all,除了设置了allow的ip,其余都无法访问
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;		#绑定域名

#    ssl_certificate      cert.pem;   #放证书pem文件位置
#    ssl_certificate_key  cert.key;		#放证书key文件位置

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}

目录索引、下载服务

mkdir /download
yum install python3 python3-devel --downloadonly --downloaddir=/download
vim /etc/nginx/conf.d/autoindex.conf
server {
listen 11111;
server_name _;
location / {
autoindex on;              # *** 设置目录索引
autoindex_localtime on;    # ***
autoindex_exact_size off;  # ***
root /download;
}
}

链接数监控

该功能需要用到http_stub_status_module模块,默认nginx不支持这个功能,可通过nginx -V去查看是否安装
创建nginx的虚拟主机文件,单独的去测试这个status状态功能即可
如果你的nginx默认不支持这个status功能,
重新编译nginx二进制命令,把这个模块添加进去

vim status.conf

server{
listen 9999;
server_name _;
	stub_status on;
	access_log off; # 因为它不是一个基于http请求响应的网站,仅仅是展示连接的信息,都不需要写location。
}

测试访问
[root@test /etc/nginx/conf.d]#curl 10.0.0.200:9999
Active connections: 1 
server accepts handled requests
10134 10134 10388 
Reading: 0 Writing: 1 Waiting: 0 

Active connections 前活动客户端连接数,包括Waiting连接数。
accepts 接受的客户端连接总数。
handled 处理的连接总数。
accepts 通常,除非已达到某些资源限制(例如worker_connections限制),否则该参数值相同。
requests 客户端请求的总数。
Reading nginx正在读取请求标头的当前连接数。
Writing nginx将响应写回客户端的当前连接数。
Waiting 当前等待请求的空闲客户端连接数。

# 注意
一个tcp连接,可以发起多个http请求,可以通过修改保持连接参数修改
keepalive_timeout 0; 表示关闭长连接

利用ab命令测试
ab -c 100 -n 100000 http://10.0.0.200/

允许和限制访问

基于ip地址的访问限制

匹配规则是自上而下的匹配,匹配上一条规则后则不再继续匹配后续规则,allow和deny的顺序要先理清楚

- 限制只允许10.0.0.0~10.0.0.255范围的IP访问(禁止其他网段的访问)
server {
listen 22334;
server_name _;
location / {
allow 10.0.0.0/24;   # 允许通过的网段,但是不是只允许,因此这条规则无法限制其他网段不能访问
deny all;       # 这条规则限制了所有网段都不能访问,但是匹配上了第一条规则的ip不再匹配这一条
root /www/deny-allow;
index index.html;
}
}

- 限制拒绝10.0.0.0~10.0.0.255范围的IP访问(得允许其他网段访问)
server {
listen 22334;
server_name _;
location /  {
	deny 10.0.0.0/24;
	root  /www/deny-allow;
	index index.html;
	}
}

设置网页密码访问

1、先下载密码生成文件工具
yum install -y httpd-tools
2、生成密码
# -b 免交互,输入账号密码即可
# -c 设置密码写入到什么文件
htpasswd -c ./passwd.db lnmp    #./passwd.db 表示用户密码生成到该文件下   lnmp表示用户名称
htpasswd -b -c /etc/nginx/auth_passwd lnmp 123456
生成之后通过 cat ./passwd 查看用户名和密码是否生成成功
3、打开nginx配置文件
可以在server或location下配置
auth_basic "login";   #提示信息
auth_basic_user_file /usr/local/nginx/passwd.db;    #/usr/local/nginx/conf/passwd.db代表passwd.db文件存放的位置

限流配置

# 1.定义一个限速规则
# 定义限速区域,保持在10兆字节的区域one,该区域的平均处理请求每秒不能超过1个。
# $binary_remote_addr 变量的大小始终为 4 个字节,在64位机器上始终占用64字节
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req_zone $binary_remote_addr zone=two:10m rate=1r/s;

参数解释
limit_req_zone        # 引用限速模块
binary_remote_addr    # 判定条件,远程的客户端IP
zone        # 定义限速区域名称,内存大小
rate        # 限速规则,1秒只能1个请求


# 2.引用限速规则
limit_req zone=two  burst=5;  
limit_req # 引用哪一个限速区域

burst=5 # 令牌桶、平均每秒不超过 1 个请求,并且突发不超过5个请求。
nodelay  # 如果不希望排队堆积的请求过多,可以用这个参数。
针对客户端ip判断的,以及针对请求访问速率的限制模块
vim limit_req.conf 

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen   33555;
server_name  _;
charset utf-8;
access_log /var/log/nginx/limit_req.log;
limit_req zone=one burst=3 nodelay;
location / {
root   /www/limit-req; 
index  index.html index.htm;
}
}

location匹配规则

精确匹配
location = / { 
#规则
}
~  大小写敏感   区分大小写
location ~ /Example/ {
#规则
}
~* 大小写忽略
location ~* /Example/ {
#规则
}
^~ 只匹配以 uri 开头
location ^~ /img/ {
#规则
}
/  其他匹配都不成功  就匹配此项
location / {
#规则
}
location匹配优先级
(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
匹配符 匹配规则 优先级
= 定义 URI 和位置的精确匹配。 1
^~ 以某个字符串开头(不检查正则,区分大小写) 2
~ 区分大小写的正则匹配 (认识正则,区分url大小写) 3
~* 不区分大小写的正则匹配(认识正则,不区分url大小写) 4

location中的root和alias实战

server {
	listen 33555;
	server_name _;
	# 需要访问 10.0.0.8:33555/static/caixukun.jpg
	#  你有什么写法,可以返回这个数据,给用户看到呢?
	# 当前有一个代码目录,叫做 /huya/ 要求静态数据放在这个目录下
	# 已知有一个静态图片,放在如下的目录中 /huya/static/caixukun.jpg 
	# 要求,要进行静态请求匹配,匹配/static/ url开头
	# 等于匹配用户访问的url形式是  10.0.0.8:33555/static/caixukun.jpg
	location ^~ /static/ {
		# 两种写法第一个,写root,root特点是会将该url(/static)填充到网页根目录下,认为它也是一个目录
		# root  /huya/static/;  #错误写法
		root  /huya/;# 正确写法
		
		# 第二种写法,alias别名用法
		alias /huya/static/;
	}
}

隐藏nginx版本号

在http中加入一行   server_tokens off;

防盗链

#图片请求防盗链
location ~* \.(jpg|png|jpeg|gif|bmp) {
valid_referers www.shop.com;
		if ($invalid_referer) {
	    return 404;
	}

配置访问PHP后端

location ~ \.php$ {
	 如果要找具体的后端代码,在/code/去找
 root /code;
   # 会把请求转发给后端的php-fpm进行加工
   fastcgi_pass 127.0.0.1:9000;
   # 默认的首页文件名字是index.php
   fastcgi_index index.php;
   # 保留用户想请求的脚本名,以及具体的uri信息的
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		 # 导入一个文件,能够让fastcgi识别http的这些内置变量
		 # 在nginx的默认路径下,/etc/nginx/fastcgi_params
 include fastcgi_params;
}


php的代码是放在/code目录下,每次更新只需要将php写的代码解压放到/code目录下即可访问新的服务
posted @ 2023-08-31 11:44  村尚chun叔  阅读(58)  评论(0编辑  收藏  举报