当前位置: 首页 > news >正文

Nginx 配置

文章目录

  • 1、Nginx 配置
    • 1.1 全局配置
    • 1.2 I/O事件配置
    • 1.3 HTTP 配置
    • 1.4 Web服务监听配置
    • 1.5 其他配置
  • 2、访问状态统计配置
    • 2.1 查看已安装的模块
    • 2.2 修改 nginx.conf 配置文件
    • 2.3 重启服务-访问测试
  • 3、基于授权的访问控制
    • 3.1 生成用户密码认证文件
    • 3.2 修改主配置文件相对应目录,添加认证配置项
    • 3.3 重启服务-访问测试
  • 4、基于客户端的访问控制
  • 5、基于域名
    • 5.1 为虚拟主机提供域名解析
    • 5.2 为虚拟主机准备网页文档
    • 5.3 修改nginx配置文件
    • 5.4 重启服务-访问测试
  • 6、基于IP
    • 6.1 配置server1
    • 6.2 配置server 2
  • 7、基于端口
    • 7.1 配置端口1
    • 7.2 配置端口2
    • 7.3 重启服务-测试访问
  • 总结

1、Nginx 配置

1.1 全局配置

## 运行用户,若编译时未指定则默认为 nobody
## user nobody; user nginx	 
## 工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
worker_processes 4; 			
## 错误日志文件的位置
## error_log logs/error.log; 
## PID 文件的位置
pid logs/nginx.pid; 	

1.2 I/O事件配置

events {
## 使用epoll模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能use epoll; 	
## 每个进程处理 4096 个连接worker_connections 4096; 
}
## 如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
## 在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。## 可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。
/etc/security/limits.conf## epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。## 若工作进程数为 8,每个进程处理 4096 个连接,则允许 Nginx 正常提供服务的连接数已超过 3 万个(4 096×8=32768),当然具体还要看服务器硬件、网络带宽等物理条件的性能表现。

1.3 HTTP 配置

http {## 文件扩展名与文件类型映射表include       mime.types;   ##	默认文件类型default_type  application/octet-stream;##	日志格式设定## log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '##                 '$status $body_bytes_sent "$http_referer" '##                 '"$http_user_agent" "$http_x_forwarded_for"';## 访问日志位置## access_log  logs/access.log  main;   访问网站的日志## 支持文件发送(下载)sendfile        on;## 此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用## tcp_nopush     on;## 连接保持超时时间,单位是秒## keepalive_timeout  0;keepalive_timeout  65;## gzip模块设置,设置是否开启gzip压缩输出## gzip  on;

1.4 Web服务监听配置

##Web 服务的监听配置
server {##监听地址及端口listen 80; ##站点域名,可以有多个,用空格隔开server_name www.fyl.com;##网页的默认字符集charset utf-8;##根目录配置location / {##网站根目录的位置/usr/local/nginx/htmlroot html;##默认首页文件名index index.html index.php;}location /zjl {##网站根目录的位置/usr/local/nginx/htmlroot /usr/local/nginx/html/zjl;##默认首页文件名index index.html index.php;}##内部错误的反馈页面error_page 500 502 503 504 /50x.html;##错误页面配置location = /50x.html {root html;}
}
}

1.5 其他配置

 日志格式设定
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
====================================================================================
location常见配置指令,root、alias、proxy_pass
root(根路径配置):root /var/www/html
请求www.benet.com/test/1.html,会返回文件/var/www/html/test/1.htmlalias(别名配置):alias /var/www/html
请求www.benet.com/test/1.html,会返回文件/var/www/html/1.htmlproxy_pass(反向代理配置)

2、访问状态统计配置

2.1 查看已安装的模块

查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
/usr/local/nginx/sbin/nginx -V 
可查看 nginx 已安装的所有模块
cat /opt/nginx-1.20.2/auto/options | grep YES     

2.2 修改 nginx.conf 配置文件

修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......server {listen 80;server_name www.benet.com;charset utf-8;location / {root html;index index.html index.php;}##添加 stub_status 配置##location /status { 					#访问位置为/status    www.benet.com/statusstub_status on; 				#打开状态统计功能access_log off; 				#关闭此位置的日志记录}}
}

2.3 重启服务-访问测试

systemctl restart nginx浏览器访问 http://192.168.10.21/status
Active connections :表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。可 curl -s http://192.168.10.21/status 结合 awk与if 语句进行性能监控。

3、基于授权的访问控制

3.1 生成用户密码认证文件

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db

3.2 修改主配置文件相对应目录,添加认证配置项

vim /usr/local/nginx/conf/nginx.conf
......server {location / {......##添加认证配置##auth_basic "secret";				#设置密码提示框文字信息auth_basic_user_file /usr/local/nginx/passwd.db;}}

3.3 重启服务-访问测试

nginx -t
systemctl restart nginx浏览器访问 http://192.168.10.21

4、基于客户端的访问控制

访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
规则从上往下执行,如匹配则停止,不再往下匹配。vim /usr/local/nginx/conf/nginx.conf
......server {location / {......##添加控制规则##allow 192.168.10.20; 					#允许访问的客户端 IPdeny all;								#拒绝其它IP客户端访问}}systemctl restart nginx

5、基于域名

5.1 为虚拟主机提供域名解析

echo "192.168.10.21 www.yjs.com www.benet.com" >> /etc/hosts

5.2 为虚拟主机准备网页文档

mkdir -p /usr/local/nginx/html/benet   
mkdir -p /usr/local/nginx/html/yjs
echo "<h1>www.yjs.com</h1>" > /usr/local/nginx/html/yjs/index.html
echo "<h1>www.benet.com</h1>" > /usr/local/nginx/html/benet/index.html

5.3 修改nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......server {listen 80;server_name www.yjs.com;					#设置域名www.yjs.comcharset utf-8;access_log logs/www.yjs.access.log; 		#设置日志名location / {root /usr/local/nginx/html/yjs;					#设置www.yjs.com 的工作目录index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}server {listen 80;server_name www.benet.com;					#设置域名www.benet.comcharset utf-8;access_log logs/www.benet.access.log; location / {root /usr/local/nginx/html/benet;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}	
}

5.4 重启服务-访问测试

systemctl restart nginx浏览器访问
http://www.yjs.com
http://www.benet.com

6、基于IP

6.1 配置server1

ifconfig ens33:0 192.168.10.40 netmask 255.255.255.0    创建 一个ens36 或者37的网卡  192.168.10.40vim /usr/local/nginx/conf/nginx.conf
......
http {
......server {listen 192.168.10.22:80;					#设置监听地址192.168.10.22server_name www.yjs.com;charset utf-8;access_log logs/www.yjs.access.log; location / {root /usr/local/nginx/html/yjs;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}1}

6.2 配置server 2

server {listen 192.168.10.40:80;					#设置监听地址192.168.10.40server_name www.benet.com;charset utf-8;access_log logs/www.benet.access.log; location / {root /usr/local/nginx/html/benet;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}
}	
}
systemctl restart nginx浏览器访问
http://192.168.10.22
http://192.168.10.40

7、基于端口

7.1 配置端口1

vim /usr/local/nginx/conf/nginx.conf
......
http {
......server {listen 192.168.10.22:8080;					#设置监听 8080 端口server_name www.yjs.com;charset utf-8;access_log logs/www.yjs.access.log; location / {root /var/www/html/yjs;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}}

7.2 配置端口2

server {listen 192.168.10.22:8888;					#设置监听 8888 端口server_name www.benet.com;charset utf-8;access_log logs/www.benet.access.log; location / {root /var/www/html/benet;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = 50x.html{root html;}
}	

7.3 重启服务-测试访问

systemctl restart nginx浏览器访问
http://192.168.10.21:8080
http://192.168.10.21:8888

总结

在这里插入图片描述


文章转载自:

http://jhl2NFp4.dkqbc.cn
http://1RrZ4vQF.dkqbc.cn
http://oOHuhBVH.dkqbc.cn
http://EI9f5GBy.dkqbc.cn
http://eMBH5Lo0.dkqbc.cn
http://BR6TJqCi.dkqbc.cn
http://0ODJ5793.dkqbc.cn
http://vxGksJxy.dkqbc.cn
http://UWvVpjnE.dkqbc.cn
http://E5Q1Ho8r.dkqbc.cn
http://70Aqo32L.dkqbc.cn
http://7HhwVUlp.dkqbc.cn
http://27ERqzHL.dkqbc.cn
http://iif5zYgm.dkqbc.cn
http://AZwWsYwH.dkqbc.cn
http://qQ2BpGEe.dkqbc.cn
http://crF6hA5J.dkqbc.cn
http://AO4rjkU4.dkqbc.cn
http://JIy6aXgp.dkqbc.cn
http://dTUSmfC1.dkqbc.cn
http://ydUgaBSm.dkqbc.cn
http://dtwCGRzn.dkqbc.cn
http://44crPTvb.dkqbc.cn
http://b0vW2vPh.dkqbc.cn
http://or5blCfJ.dkqbc.cn
http://aPx6hNwJ.dkqbc.cn
http://1KubPFsZ.dkqbc.cn
http://MfDNQ42a.dkqbc.cn
http://VDhhqDoV.dkqbc.cn
http://EzVYOvr7.dkqbc.cn
http://www.dtcms.com/a/374590.html

相关文章:

  • 20250910_《SQL Server 数据库事务日志定期清理方案(精简优化版)》以10.1.1.31服务器的gtp-default数据库为例
  • 多输入(input)多输出(output)验证
  • 排查JSch连接SFTP服务器失败的问题
  • JMeter压测过程中监控服务器CPU及内存的方法
  • 整理python快速构建数据可视化前端的Dash库
  • Redis缓存穿透、缓存击穿与雪崩防护及性能优化实战指南
  • ArcGIS学习-20 实战-地形研究
  • Ubuntu下基于Nginx+ffmpeg+video.js的HLS流媒体视频播放方案
  • Vue2 VS Vue3
  • 【ArcGIS】如何编辑图层的属性表
  • VueFlow的箭头怎么调整
  • 基于Vue3 +ElementuiPlus + Dexie.js自研的浏览器插件新建标签页tab
  • 【序列晋升】30 Spring Cloud Vault 安全配置管理的微服务守护者
  • 狂想-一种新颖的低成本内嵌标记的视触觉感知前导方案
  • 兰洋科技双展联动展示液冷创新成果,技术驱动打造绿色算力新基建
  • INDEMIND亮相2025科技创变者大会,以机器人空间智能技术解锁具身智能新边界
  • 百度SEM里什么是搜索广告、搜索词、否定关键词、上方位(竞价)广告?
  • 百度竞价推广:百度搜索竞价推广代运营
  • rabbitmq如何保证消息不丢失
  • 做百度SEM付费搜索推广时,竞价账号定向怎么设置?
  • html+css+JavaScript实现一个简单的登录
  • 【国内电子数据取证厂商龙信科技】从SQL语句开始数据库分析
  • 字节跳动Seed推出「机器人大脑」Robix:让机器人学会思考、规划与灵活互动
  • 【ComfyUI】Flux Schnell Fp8量化版图像生成
  • 【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
  • 从零开始的云计算生活——第六十天,志在千里,使用Jenkins部署K8S
  • 平板热点频繁断连?三步彻底解决
  • nand flash的擦除命令使用
  • 《Pod调度失效到Kubernetes调度器的底层逻辑重构》
  • OC-单例模式