做网站公司负责修图吗,网页设计教程视屏,河南省水利建设厅网站,现在网站做多宽的一、六个模块的作用 全局块#xff1a;全局配置#xff0c;对全局生效#xff1b; events块#xff1a;配置影响 Nginx 服务器与用户的网络连接#xff1b; http块#xff1a;配置代理#xff0c;缓存#xff0c;日志定义等绝大多数功能和第三方模块的配置#xff1b;…一、六个模块的作用 全局块全局配置对全局生效 events块配置影响 Nginx 服务器与用户的网络连接 http块配置代理缓存日志定义等绝大多数功能和第三方模块的配置 server块配置虚拟主机的相关参数一个 http 块中可以有多个 server 块 location块用于配置匹配的 uri upstream配置后端服务器具体地址负载均衡配置不可或缺的部分。
二、认识Nginx服务的主配置文件
2.1 全局配置模块
是配置文件从头开始到 events 块之间的内容主要设置的是影响nginx服务器整体运行的配置指令。比如 worker_process值越大可以支持的并发处理量也越多但是还是和服务器的硬件相关。
vim /usr/local/nginx/conf/nginx.conf #主配置文件位置 #user nobody; #运行用户若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量一般设置为和 CPU 核数一样设置为autonginx将会自己获取这个数值
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置
worker_rlimit_nofile 60000; #设置所有worker进程最大可以打开的文件数默认为10242.2 I/O 事件配置 events {use epoll; #使用 epoll I/O模型2.6及以上版本的系统内核建议使用epoll模型以提高性能worker_connections 60000; #每个进程处理 60000 个连接默认是1024个multi_accept on; #是否一次性将监听到的连接全接收进来默认为off关闭时一次接收一条连接accept_mutex on; #默认为on开启时表示以串行方式接入新连接否则将通报给所有worker。这可能会浪费资源并产生不可预计的后果例如惊群问题
}
如果提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
永久修改的方式
[rootCXK /opt/nginx-1.24.0/conf]#vim /etc/security/limits.conf #在Linux平台上在进行高并发TCP连接处理时最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄每个socket句柄同时也是一个文件句柄)。#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。#epoll是Linux内核为处理大批句柄而作改进的poll是Linux下多路复用IO接口select/poll的增强版本它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。实现异步非阻塞 2.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; ##开启文件传输模式#tcp_nopush on; ##只在sendfile on时有效。调用tcp_cork方法让数据包不会马上传送出去等到数据包最大时一次性的传输出去这样有助于解决网络堵塞。默认为off。#keepalive_timeout 0; ##连接保持超时时间单位是秒keepalive_timeout 65; ##连接保持超时时间单位是秒#gzip on; ##gzip模块设置设置是否开启gzip压缩输出 2.4 web服务监听设置 server {listen 80; ##监听地址及端口server_name www.kgc.com; ##站点域名可以有多个用空格隔开charset utf-8; ##网页的默认字符集location / { ##根目录配置root html; ##网站根目录的位置/usr/local/nginx/htmlindex index.html index.php; ##默认首页文件名}error_page 500 502 503 504 /50x.html; ##内部错误的反馈页面location /50x.html { ##错误页面配置root html;}}
} 2.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
处理方式: root路径location路径 请求www.kgc.com/test/1.html会返回文件/var/www/html/test/1.html
alias别名配置alias /var/www/html
处理方式alias路径替换location路径 请求www.yang.com/test/1.html会返回文件/var/www/html/1.html
root和alias对比 当设置 location /test{ } alias /var/www/html 和 root /var/www/html 有什么区别 alias是别名设置将设置的网页放在/var/www/html下访问 root 是根目录设置 将设置的网页放在 /var/www/html/test 下访问 三、nginx访问状态统计
1. 查看访问统计配置的相关模块
cat /opt/nginx-1.24.0/auto/options | grep YES #可查看 nginx 已安装的所有模块 [rootCXK ~]#/usr/local/nginx/sbin/nginx -V 或者nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块2. 修改主配置文件添加访问状态统计模块
#主配置备份防止设置错误无法还原
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak 修改主配配置具体操作 vim /usr/local/nginx/conf/nginx.confserver {listen 80;server_name www.cxk.com; #主机名设置charset utf-8; #万国字符集#access_log logs/host.access.log main;location / {root html; #Nginx默认网页设置index index.html index.htm;}#访问位置为/statuslocation /status {stub_status on; #打开状态统计功能access_log off; #关闭此位置的日志记录}3.重启nginx服务访问测试
nginx -t #检查nginx配置文件是否有错误
kill -1 11799四、nginx授权访问控制 1. 生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db lisi
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db 2. 修改主配置
vim /usr/local/nginx/conf/nginx.confserver {location / {......##添加认证配置##auth_basic secret; #设置密码提示框文字信息auth_basic_user_file /usr/local/nginx/passwd.db; #密码认证模块的加载配置}} 3. 重启服务进行访问测试
nginx -t #检查nginx配置文件是否有错误
kill -1 11799五、nginx客户端访问控制 设置方式类似于黑白名单 1. 设置前的访问其他主机访问测试 访问控制规则如下deny IP/IP 段拒绝某个 IP 或 IP 段的客户端访问。 allow IP/IP 段允许某个 IP 或 IP 段的客户端访问。 规则从上往下执行如匹配则停止不再往下匹配。
2. 修改主配置
vim /usr/local/nginx/conf/nginx.conf
......server {location / {......##添加控制规则##allow 192.168.136.100; #允许当前主机allow 127.0.0.1; #允许自己allow 192.168.136.1; #允许真机allow 192.168.136.120; #允许访问的客户端 IPdeny all; #拒绝其它IP客户端访问}} 3. 重启服务进行访问测试
nginx -t #检查nginx配置文件是否有错误
kill -1 11799 六、nginx虚拟主机
6.1 基于域名的虚拟主机
1为虚拟主机提供域名解析 echo 192.168.136.100 www.cxk.com www.kun.com /etc/hosts 添加到本机域名解析
2.为虚拟主机准备网页文档 mkdir -p /var/www/html/cxk #分别创建两个不同域名的网页目录
mkdir -p /var/www/html/kun #分别创建两个不同域名的网页目录
echo h1cxk hhh/h1 /var/www/html/cxk/index.html #为两个不同的域名默认首页网页添加内容
echo h1kun hhh/h1 /var/www/html/kun/index.html #为两个不同的域名默认首页网页添加内容
3.修改Nginx的配置文件 vim /usr/local/nginx/conf/nginx.conf
server {listen 80;server_name www.cxk.com;charset utf-8;access_log logs/www.cxk.access.log; #不同虚拟主机的域名访问日志location / {root /var/www/html/cxk; #默认网页index index.html index.htm; }#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;}
}server {listen 80;server_name www.kun.com;charset utf-8;access_log logs/www.kun.access.log;location / {root /var/www/html/kun;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location /50x.html {root html;}4.重启服务测试
systemctl restart nginx 6.2 基于IP 的 Nginx 虚拟主机 1. 设置虚拟主机IP
[rootlocalhost conf]#ifconfig ens33:0 192.168.136.200/24
[rootlocalhost conf]#ifconfig ens33:0 2. 修改主配置文件
vim nginx.confserver {listen 192.168.136.100:80;server_name localhost;charset utf-8;access_log logs/www.cxk.access.log;location / {root /var/www/html/cxk;index index.html index.htm;}#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;}
}
server {listen 192.168.136.200:80;server_name localhost;charset utf-8;access_log logs/www.kun.access.log;location / {root /var/www/html/kun;index index.html index.htm;}
error_page 500 502 503 504 /50x.html;location /50x.html {root html;}3. 重启服务测试 6.3 基于端口的 Nginx 虚拟主机
1. 修改主配置文件
vim nginx.conf
server {listen 192.168.136.100:8080;server_name localhost;charset utf-8;access_log logs/www.cxk.access.log;location / {root /var/www/html/cxk;index index.html index.htm;}#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;}
}
server {listen 192.168.136.200:8848;server_name localhost;charset utf-8;access_log logs/www.kun.access.log;location / {root /var/www/html/kun;index index.html index.htm;}
error_page 500 502 503 504 /50x.html;location /50x.html {root html;}2. 重启测试