网站开发可以自学吗,平台外宣推广技巧,wordpress程序备份,有没有可以做游戏的网站吗文章目录 1. Nginx 配置文件1.1 主配置文件1.2 子配置文件1.3 全局配置1.3.1 修改启动的进程数1.3.2 cpu和work进程绑定#xff08;nginx调优#xff09;1.3.3 修改PID路径1.3.4 nginx进程的优先级#xff08;work进程的优先级#xff09;1.3.5 调试work进程打开的文件的个… 文章目录 1. Nginx 配置文件1.1 主配置文件1.2 子配置文件1.3 全局配置1.3.1 修改启动的进程数1.3.2 cpu和work进程绑定nginx调优1.3.3 修改PID路径1.3.4 nginx进程的优先级work进程的优先级1.3.5 调试work进程打开的文件的个数1.3.6 只有 master进程没有 work进程 (仅测试用) 2. event 事件3. http设置3.1 http事件类型3.2 mime3.3 sever 下的 root3.4 server块构建虚拟主机3.4.1 基于域名3.4.2 基于端口3.4.3 基于IP地址 3.5 路径别名-----alias3.6 localion模块3.6 语法规则 3.7 基于四层的访问控制 ----- access模块3.7.1 IP地址访问限制允许或拒绝特定的IP地址访问3.7.2 请求方法访问控制允许或拒绝特定HTTP请求方法3.7.3 URI访问控制允许或拒绝特定URI或URI模式的访问 3.8 验证模块3.8.1 htpasswd命令3.8.2 示例 3.9 自定义错误页面3.10 修改日志位置存放3.10.1 示例 3.11 检测文件是否存在3.12 keepalive ---长连接3.12.1 keepalive_timeout3.12.2 keepalive_requests3.12.3 keepalive_disable 3.13 作为下载服务器配置3.13 其他设置 1. Nginx 配置文件
1.1 主配置文件
主配置文件nginx.conf
#主配置文件格式main block主配置段即全局配置段对http,mail都有效#事件驱动相关的配置 同步
event {...
}
#http/https 协议相关配置段
http {...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {...
}
#stream 服务器相关配置段
stream {负载均衡...
}#yum安装的nginx.conf示例
vim /etc/nginx/nginx.conf1.2 子配置文件
子配置文件: include conf.d/*.confss
子配置文件一般在主配置文件的http部分。
http块中可以包含多个子配置文件常见的子配置文件
server块用于配置HTTP服务器的具体行为包括监听的端口、虚拟主机的配置、请求处理逻辑等。
location块用于指定不同URL请求的处理方式例如静态文件的服务、反向代理等。
upstream块用于配置反向代理的目标服务器列表。
include指令用于引入其他的子配置文件可以将一些通用的配置项单独放在一个文件中然后通过include指令引入。
1.3 全局配置
1.3.1 修改启动的进程数
通过使用 auto 参数Nginx 可以根据系统的负载情况智能地分配工作进程以提供更好的性能和资源利用率。
修改配置文件中worker_processes项vim /apps/nginx/conf/nginx.conf
#编辑主配置文件nginx -s reopen
#重新加载ps axo pid,cmd,psr,ni|grep nginx
#查看nginx的 worker数量1.3.2 cpu和work进程绑定nginx调优
将Nginx工作进程绑定到指定的CPU核心极大减少了nginx的工作进程在不同的cpu核心上的来回跳转减少了CPU对进程的资源分配与回收以及内存管理等因此可以有效的提升nginx服务器的性能
worker_cpu_affinity 块指令用于控制 worker 进程与 CPU 的亲和性vim /apps/nginx/conf/nginx.conf
#编辑配置文件user nginx;
worker_processes 2;
worker_cpu_affinity 00000001 00000010;
#绑定到 第一 和 第二块cpu上
error_log /apps/nginx/logs/error.log;
#指定报错文件的路径nginx -t
#语法检查ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n1.3.3 修改PID路径
vim /app/nginx/conf/nginx.conf1.3.4 nginx进程的优先级work进程的优先级
当你想将nginx的work进程的优先级调高 可以使用nice设置
nice的优先级是 -20 到 19worker_priority 0;
#工作进程优先级-20~20(19)#首先查看优先级ps axo pid,cmd,psr,ni|grep nginx|sort -n
#查看默认优先级默认优先级为0worker_priority -20;
#添加此项nginx -t
#语法检查nginx -s reload
#重新加载ps axo pid,cmd,psr,ni|grep nginx|sort -n
#查看优先级1.3.5 调试work进程打开的文件的个数
所有worker进程能打开的文件数量上限,包括:Nginx的所有连接例如与代理服务器的连接等而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致,只要机器性能够多加几个也没问题
vim /apps/nginx/conf/nginx.conf
#首先修改主配置文件#添加
worker_rlimit_nofile 65536; 临时修改
ulimit -n 30000
#修改单个进程能打开的最大文件数为 30000
#仅应用于当前会话即不会永久修改限制ulimit -a
#显示当前用户的所有资源限制信息永久修改需要修改pam认证模块
vim /etc/security/limits.conf
#最后加入* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
* hard msgqueue 8192000nproc最大进程数限制的软限制和硬限制都设置为 1000000当前用户在单个会话中可以创建的最大进程数为 1000000nofile打开文件描述符限制的软限制和硬限制都设置为 1000000这意味着当前用户在单个会话中可以使用的最大文件描述符数将被限制为 1000000。软限制是软性限制用户可以根据需要进行调整而硬限制是硬性限制一旦设定用户无法超过该限制memlock锁定内存限制的软限制和硬限制都设置为 32000这意味着当前用户在单个会话中可以锁定的最大内存量为 32000KBmsgqueue消息队列限制的软限制和硬限制都设置为 8192000这意味着当前用户在单个会话中可以使用的最大消息队列大小为 8192000字节reboot
#重启生效ulimit -a
#显示当前用户的所有资源限制信息1.3.6 只有 master进程没有 work进程 (仅测试用)
实际生产中使用较少
master_process off|on;
#是否开启Nginx的master-worker工作模式仅用于开发调试场景,默认为on2. event 事件
在Nginx的主配置文件中events事件用于配置Nginx服务器的事件模块相关参数控制Nginx服务器在处理连接请求时的行为
常见配置参数 worker_connections指定每个工作进程可以同时处理的最大连接数。 multi_accept指定是否一次接受多个连接。默认情况下Nginx在每个循环中只接受一个连接但设置multi_accept为on后可以同时接受多个连接。 use指定Nginx使用的事件模块。常见的事件模块有epoll、kqueue和eventport等。
#示例
events {worker_connections 2048; #设置单个工作进程的最大并发连接数use epoll;accept_mutex on; onmulti_accept on;
#指定了每个工作进程可以处理的最大连接数为2048启用了多个连接同时接受以及使用了epoll事件模块3. http设置
3.1 http事件类型
include引入其他配置文件通常用于加载 MIME 类型配置文件。
default_type指定默认的 MIME 类型。
server定义一个或多个虚拟主机。
listen指定该虚拟主机监听的端口。
server_name指定域名用于匹配请求的主机头。
root指定虚拟主机的根目录。
location用于匹配不同的 URL并定义相关配置规则。
#基础格式
http {...... #各server的公共配置server { #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器...}server { ...server_name #虚拟主机名root #主目录alias #路径别名location [OPERATOR] URL { #指定URL的特性...if CONDITION {...}}}
}#协议配置详解
http {include mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录default_type application/octet-stream; #除mime.types中文件类型外,设置其它文件默认类型访问其它类型时会提示下载不匹配的类型文件
#日志配置部分#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的情况下合并请求后统一发送给客户端。#tcp_nodelay off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项当为off时延迟0.2s发送默认On时不延迟发送立即发送用户响应报文。#keepalive_timeout 0;keepalive_timeout 65 65; #设置会话保持时间,第二个值为响应首部:keepAlived:timeout65,可以和第一个值不同#gzip on; #开启文件压缩server {listen 80; #设置监听地址和端口server_name localhost; #设置server name可以以空格隔开写多个并支持正则表达式如:*.kgc.com www.kgc.* ~^www\d\.kgc\.com$ default_server #charset koi8-r; #设置编码格式默认是俄语格式建议改为utf-8#access_log logs/host.access.log main;location / {root html;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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ { #以http的方式转发php请求到指定web服务器# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ { #以fastcgi的方式转发php请求到php处理# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apaches document root# concurs with nginxs one##location ~ /\.ht { #拒绝web形式访问指定文件如很多的网站都是通过.htaccess文件
来改变自己的重定向等功能。# deny all;#}location ~ /passwd.html {deny all;}}# another virtual host using mix of IP-, name-, and port-based configuration##server { #自定义虚拟server
3.3.1 MIME
范例: 识别php文件为text/html# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / { # root html;# index index.html index.htm; #指定默认网页文件此指令由
ngx_http_index_module模块提供# }#}# HTTPS server##server { #https服务器配置# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.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;# }#}3.2 mime
在Nginx中“mime” 是一种配置指令用于设置 MIME 类型与文件扩展名的映射关系
vim /etc/nginx/mime.types
#查看当前nginx配置mime类型表3.3 sever 下的 root
在Nginx配置中root指令用于设置服务器块server block的根目录即指明软件的根目录。
通常root指令位于Nginx配置文件中的服务器块server block中。
server {listen 80;server_name example.com;root /var/www/html;location / {...}...
}
#指定了服务器块的根目录为/var/www/html
#访问该服务器的网站时Nginx会在/var/www/html文件夹中查找并提供相应的静态文件3.4 server块构建虚拟主机
以PC端和手机端示例
3.4.1 基于域名
vim /etc/nginx/nginx.conf
#修改配置文件 要放在 http 模块里#添加
include /apps/nginx/conf.d/*.conf;mkdir -p /apps/nginx/conf.d/
#建立子配置文件vim pc.conf
vim mobile.conf
#分别编写PC端和手机端配置文件#PC端
server{listen 192.168.67.100:80;server_name www.pc.com;location / {root /data/nginx/html/pc;}
}#mobile端
server {listen 80;server_name www.mobile.com;root /data/nginx/html/mobile/;
}
mkdir /data/nginx/html/pc -pv
mkdir /data/nginx/html/mobile -pv
#构建数据文件夹echo pc /data/nginx/html/pc/index.html
echo moblie /data/nginx/html/mobile/index.html#构建数据文件##切换到测试机
#编辑本地hosts文件添加地址映射vim /etc/hosts#测试是否成功
curl www.mobile.com
curl www.pc.com3.4.2 基于端口
服务端
#编辑子配置文件 mobile.conf修改端口号
server{listen 192.168.67.100:8080;server_name www.mobile.com;root /data/nginx/html/moblie;}#computer.conf 不变
server{listen 192.168.67.100:80;server_name www.pc.com;root /data/nginx/html/pc;
}#测试
curl 192.168.67.100:80
curl 192.168.67.100:80803.4.3 基于IP地址
#服务端
#配置新网卡#编辑子配置文件mobile.conf修改IP地址
cd /apps/nginx/conf.d/
vim mobile.confserver{listen 192.168.67.155:80;server_name www.mobile.com;root /data/nginx/html/mobile;}#computer.conf不变
server{listen 192.168.67.100:80;server_name www.pc.com;root /data/nginx/html/pc;
}nginx -t reload
#重新加载#测试
curl 192.168.67.155
curl 192.168.67.1003.5 路径别名-----alias
在 Nginx 中alias 用于创建一个路径别名的指令。
别名可以用于将文件或目录从一个位置映射到另一个位置提供更灵活的访问控制
server {listen 192.168.67.155:80;server_name www.mobile.com;location /test {alias /data/nginx/html/mobile;
}
}3.6 localion模块
在一个server中location配置段可存在多个用于实现从uri到文件系统的路径映射
#官方帮助
http://nginx.org/en/docs/http/ngx_http_core_module.html3.6 语法规则
#格式
location [ | ~ | ~* | ^~ ] uri { ... } #用于标准uri前需要请求字串与uri精确匹配大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ #用于标准uri前表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查不区分字符大小写
~ #用于标准uri前表示包含正则表达式,并且区分大小写
~* #用于标准uri前表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri\ #用于标准uri前表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号#匹配优先级从高到低
, ^~, ~/~*, 不带符号
#示例
location / {[ configuration A ]
}
location / {[ configuration B ]
}
location /documents/ {[ configuration C ]
}
location ^~ /images/ {[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {[ configuration E ]
}访问路径是 / # A B 能匹配A优先级高 A
访问路径是 /index.html #只有B
访问路径是 /documents/document.html #B C能匹配 C精确度高 所以C
访问路径是 /images/1.gif # B D E 能匹配,D的优先级高D
访问路径是 /documents/1.jpg # B C E 能匹配 E3.7 基于四层的访问控制 ----- access模块
Nginx的access模块允许用户定义基于IP地址、请求方法、URI等条件的访问规则以控制客户端对NGINX服务器上特定资源的访问
3.7.1 IP地址访问限制允许或拒绝特定的IP地址访问
location / {deny 192.168.67.1;
}
#仅拒绝192.168.67.1访问location / {allow 192.168.67.0/24;deny all;
}
#仅允许192.168.67.0网段访问3.7.2 请求方法访问控制允许或拒绝特定HTTP请求方法
if ($request_method ! GET) {return 403;
}
#所有不是GET方法的访问返回4033.7.3 URI访问控制允许或拒绝特定URI或URI模式的访问
location /image {deny all;
}
#拒绝特定URI的访问
location ~ ^/www/ {allow all;
}
#允许特定URI模式的访问3.8 验证模块
3.8.1 htpasswd命令
yum install httpd-tools -y
#安装命令#常用命令
-c 代表新建用户名和密码对应的文件
-b 将密码跟在用户名后第一次
htpasswd -c 文件路径 姓名 交互式生成密码
htpasswd -bc 文件路径 姓名 密码 直接将密码跟在后面 非第一次
htpasswd 文件路径 姓名 交互式生成密码
htpasswd -b 文件路径 姓名 密码 直接将密码跟在后面 3.8.2 示例
vim /apps/nginx/conf.d/pc.conf
#编辑子配置文件#配置验证模块
server {listen 192.168.67.100:80;server_name www.scj.com;location / {root /data/nginx/html/pc;}location /admin{root /data/nginx/html/pc;auth_basic admin site;#提示信息不是所有浏览器都有用auth_basic_user_file /apps/nginx/conf.d/.httpuser;#密码文件存放位置}
}nginx -s reload
#重新加载htpasswd -bc /apps/nginx/conf.d/.httpuser scj 123456
#创建一个.htpasswd文件并添加一个使用Basic认证的用户名和密码cat /apps/nginx/conf.d/.httpuser
#查看是否创建成功#验证
打开虚拟机内置火狐浏览器
192.168.67.100/admin3.9 自定义错误页面
vim /apps/nginx/conf/nginx.conf
~~~
配置文件中可用位置http, server, location, if in location
~~~bash
#格式
error_page code ... [[response]] uri;
页面错误代码
error_page 固定写法
code 响应码可以将响应码转换
uri 访问连接示例
#新建报错显示目录和文件
mkdir /data/nginx/html/pc/error/
cd /data/nginx/html/pc/error/vim error.html #错误页面vim /apps/nginx/conf.d/pc.conf
#编辑子配置文件自定义 错误码
server {listen 192.168.67.100:80;server_name www.scj.com;root /data/nginx/html/pc;error_page 404 302 /error.html;#把错误码 404 指定成302 注意此处的error.html 需要真实存在建立的页面必须一致location /error.html {root /data/nginx/html/pc/error/;}location / {root /data/nginx/html/pc;}}nginx -t reload
#重新加载在随便一个浏览器随便输入一个错误的连接观察页面3.10 修改日志位置存放
通过修改日志的路径可以实现不同网站的日志单独存放
3.10.1 示例
mkdir /data/nginx/logs
#新建日志存放目录#编辑子配置文件指定日志存放位置
vim /apps/nginx/conf.d/mobile.confserver{listen 80;server_name www.mobile.com;root /data/nginx/html/mobile/;error_log /data/nginx/logs/m_error.log;access_log /data/nginx/logs/m_access.log;
}vim /apps/nginx/conf.d/pc.confserver{listen 80;server_name www.pc.com;root /data/nginx/html/pc;error_log /data/nginx/logs/pc_error.log;access_log /data/nginx/logs/pc_access.log;
}nginx -s reload
#重新加载3.11 检测文件是否存在
Nginx 的 try_files 指令用于指定在资源文件不存在时如何处理请求。 默认开启 用于server、location
#语法格式
#方式一
try_files file ... uri;file 表示要尝试的文件路径
uri 则表示当文件不存在时转发请求的路径。#方式二
try_files file ... code;code 表示文件不存在时 返回的状态码
#只会返回指定的 HTTP 响应码而不会转发请求到指定的 uri示例
#服务端
mkdir /data/nginx/html/pc/about
#新建寻找失败跳转页面echo default page /data/nginx/html/pc/about/default.htmlvim /apps/nginx/conf.d/pc.conf
#修改配置文件
server{
listen 192.168.67.100:80;
server_name www.pc.com;
root /data/nginx/html/pc;
location / {
root root /data/nginx/html/pc;
try_files $uri $uri.html $uri/index.html
/about/default.html;
}
}#建立测试文件
cd /data/nginx/html/pc/;touch test
echo find it test #客户端
curl www.pc.com/test
#查找一个存在的文件curl www.pc.com/tes
#查找一个不存在的文件3.12 keepalive —长连接
HTTP Keep-Alive 功能用于实现长连接允许客户端和服务器之间的 TCP 连接在发送完一个请求后保持打开状态以便在同一连接上发送多个请求和响应。
可以加在全局或者 server 。
keepalive 配置指令仅对 HTTP/1.0 和 HTTP/1.1 版本的连接有效。
对于 HTTP/2 连接keepalive 功能是默认启用的并且无需额外配置。
3.12.1 keepalive_timeout
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长0表示禁止长连接默认为75s通常配置在http字段作为站点全局配置示例
keepalive_timeout 60 65;
#开启长连接后返回客户端的会话保持时间为60s单次长连接累计请求达到指定次数请求或65秒就会被断开后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s如不设置客户端将不显示超时时间。当一个客户端与服务器之间的连接完成一个请求后的等待时间。 如果在这个时间内没有收到新的请求服务器会关闭连接。 这个时间是以秒为单位的默认值是 75 秒。
3.12.2 keepalive_requests
keepalive_requests number;
#在一次长连接上所允许请求的资源的最大数量默认为100次示例
keepalive_requests 3;
#最大三个连接3.12.3 keepalive_disable
keepalive_disable none | browser ...;
#对哪种浏览器禁用长连接3.13 作为下载服务器配置
ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求并生成目录列表,可以做为下载服务配置使用
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
#官方文档autoindex on | off;
#自动文件索引功能默为off
autoindex_exact_size on | off;
#计算文件确切大小单位bytesoff 显示大概大小单位K、M)默认on
autoindex_localtime on | off ;
#显示本机时间而非GMT(格林威治)时间默认off
autoindex_format html | xml | json | jsonp;
#显示索引的页面文件风格默认html
limit_rate rate;
#限制响应客户端传输速率(除GET和HEAD以外的所有方法)单位B/s,即bytes/second默认值0,表示无限制,此指令由ngx_http_core_module提供
set $limit_rate
#变量提供 限制 变量优先级高示例
location /download {autoindex on;#开启下载服务器autoindex_exact_size on;#开启确切大小不建议开启autoindex_localtime on;#使用当地时间limit_rate 1024k;#所有人限速1024k默认单位是字节数set $limit_rate 2M;#谁先生效alias /opt/download;}3.13 其他设置
open_file_cache off; #是否缓存打开过的文件信息
open_file_cache maxN [inactivetime];
#nginx可以缓存以下三种信息
(1) 文件元数据文件的描述符、文件大小和最近一次的修改时间
(2) 打开的目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息
maxN#可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used最近最少使用)算法实现管理
inactivetime#缓存项的非活动时长在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses
#指令所指定的次数的缓存项即为非活动项将被删除
open_file_cache_valid time;
#缓存项有效性的检查验证频率默认值为60s
open_file_cache_errors on | off;
#是否缓存查找时发生错误的文件一类的信息,默认值为off
open_file_cache_min_uses number;
#open_file_cache指令的inactive参数指定的时长内至少被命中此处指定的次数方可被归类为活动项,默认值为1范例
open_file_cache max10000 inactive60s;
#最大缓存10000个文件非活动数据超时时长60s
open_file_cache_valid 60s;
#每间隔60s检查一下缓存数据有效性
open_file_cache_min_uses 5;
#60秒内至少被命中访问5次才被标记为活动数据
open_file_cache_errors on;
#缓存错误信息limit_except method ... { ... }仅用于location
#限制客户端使用除了指定的请求方法之外的其它方法
method:GET, HEAD, POST, PUT, DELETEMKCOL, COPY, MOVE, OPTIONS, PROPFIND,
PROPPATCH, LOCK, UNLOCK, PATCH
limit_except GET {allow 192.168.67.101;deny all;
}
#除了GET和HEAD 之外其它方法仅允许192.168.67.0/24网段主机使用