nginx配置文件
nginx.conf
1、全局模块
worker_processes 1;
工作进程数,设置成服务器内核的2倍(一般不超过8个,超过8个反而会降低性能,4个,1-2个)
处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量
linux默认打开的文件数就是1024个
vim /etc/security/limits.conf
* soft nproc 65535
#能打开的进程最大数的软限制是65535,65535是最大数
* hard nproc 65535
#硬限制
* soft nofile 65535
#进程打开文件数的最大值65535
* hard nofile 65535
#设置所有用户的打开文件数(nofile)的硬限制
#配置要生效,只能重启,这是系统初始化的一个环节
root和alias之间匹配工作目录的区别
root的匹配模式 拼接
root的工作目录,访问的url /xy102
location /xy102
/opt/test1/xy102
alias匹配nginx的工作目录,路劲是绝对路径
location /xy102
alias /opt/test1/xy102
alias 只能写在http模块当中server模块的location模块里面
root可以写在server模块,也可以在http也可以在location中
alias匹配工作目录,不能够使用重定向功能
user nginx nginx;
##默认的程序用户就是nginx,这里可以保持注释无需修改
#表示主进程master会由root创建,子进程会由nginx用户来创建
worker_processes 4;
#表示进程有2个,这里和cpu数挂钩,不绑定cpu的话,进程可能会在两个cpu之间来回切换使用。浪费资源
#绑定cpu,避免进程切换
worker_cpu_affinity 0001 0010 0100 1000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/run/nginx.pid;
#pid文件的位置
events {
worker_connections 10000;
}
#events模块,决定了nginx能够处理的连接数,连接数和worker_processes的数值相乘
#http 转发和处理http请求,设置代理(正向代理,反向代理),缓存,定义日志格式,重定向配置
http {
include mime.types;
include /usr/local/nginx/conf.d/*.conf; #可以识别到conf.d下,只包含server模块的conf文件
#文件扩展名与文件类型的映射表。nginx能够打开的文件和支持的文件类型
default_type application/octet-stream;
server_tokens off;
#关闭版本号
#默认支持的文件类型 .html .htm
#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。访问日志的格式,error.log也是一样的格式
#access_log logs/access.log main;
#默认的访问日志存放路径
sendfile on;
#支持文件发送或者下载
#tcp_nopush on;
#默认就是异步非阻塞模式功能
#keepalive_timeout 0;
keepalive_timeout 65;
#连接保持的时间,单位是秒
client_header_timeout 80;
#客户端发送一个完整的请求头的超时时间,80秒之内没有发送一个完整的请求头,nginx返回码408(request time ou)
client_body_timeout 80;
#客户端和服务端建立连接之后,发送请求体的超时时间,客户端在80秒内没有发送任何内容,nginx返回408
gzip on;
#gizp模块,设置是否开启页面压缩功能(没啥用)
gzip_min_length 1k;
#最小压缩文件,小于等于1k的文件就不压缩了
gzip_buffers 4 64k;
#设置压缩的缓冲区,4个,每个缓冲区的大小64k
gzip_comp_level 6;
#压缩比例 1-9,数字越小,压缩的比例越小,速度越快,数字越大,压缩比例越大,速度越慢
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
#支持压缩的类型
#server模块就是开启web服务的模块
server {
listen 192.168.39.30:80;
#当有虚拟ip时,这个80端口不再监听总的80端口,而是192.168.39.30的80端口
#nginx的默认监听端口
server_name www.xy102.com;
#配置站点的域名
charset utf-8;
access_log logs/www.xy102.com.access.log;
#网页的默认字符集
#access_log logs/host.access.log main;
#网页匹配的工作目录的地址和支持的打开页面的文件类型
location / {
#location /xy102
#192.168.39.30 /usr/local/nginx/html/index.html 可以更改
root html;
#root /opt/test1
#root表示家目录。nginx默认工作目录的家目录。 /usr/local/nginx/html
#alias也是指匹配nginx的工作目录,alias匹配的是绝对路径,alias /opt/test1/xy102
index index.html index.htm;
# auth_basic "secret";
#开启用户密码验证
# auth_basic_user_file /usr/local/nginx/passwd.db;
#使用指定的加密文件
#q
#添加一个控制规则
# deny 192.168.39.40;
#拒绝指定IP地址
# allow all;
#允许所有
}
#location ~ \.(gif|jpg|png)$ {
# root html;
# expires 1d;
#设置图片的缓存时间
#}
location ~* \.(gif|jpg)$ {
valid_referers none blocked *.xy102.com xy102.com;
#允许xy102.com的网址访问图片
if ( $invalid_referer ) {
#rewrite ^/ http:www.xy102.com/error.png;
#如果不是xy102.com访问,一律跳转到倒链的提示
return 403;
}
}
location /status {
stub_status on;
#打开状态统计功能
access_log off;
#关闭access日志
}
#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$ {
# 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 Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 192.168.39.30:8888;
charset utf-8;
server_name www.luwei.com;
access_log logs/www.luwei.com.access.log;
location / {
root /var/www/html/luwei;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 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;
# 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;
# }
#}
}
总结:
全局模块
worker_processes 1;指定进程数
events模块决定了能够处理的连接数
stream4层代理模块
http模块 转发和处理http请求,设置代理(正向代理,反向代理),缓存,定义日志格式,重定向配置
在http模块当中,包含:
server模块
在http里面可以有多个server模块
在server模块当中包含:
location模块
在server当中可以有多个location
Active connections: 2
server accepts handled requests
2 2 45
Reading: 0 Writing: 1 Waiting: 1
#统计访问链接状态
location /status {
stub_status on;
#打开状态统计功能
access_log off;
#关闭access日志
}
Active connections
当前活动的连接数
server accepts handled requests
表示已经处理的连接数
三个数字,从左往右:已经处理的连接数,成功的建立连接的次数 已经处理的请求数
Reading: 0 Writing: 1 Waiting: 1
Reading 表示服务端正在从客户端读取请求的数据
writing 表示服务端正在把响应数据发送给客户端
waiting 表示有链接处于空闲状态,等待新的请求
#基于密码的授权进行访问控制
yum -y install httpd-tools
htpasswd -c /usr/localnginx/passwd.db lzl
chmod 400 passwd.db
#密码文件的权限一定是400,只有拥有者才能更改
location / {
root html;
index index.html index.htm;
auth_basic "secret";
#开启用户密码验证
auth_basic_user_file /usr/local/nginx/passwd.db;
#使用指定的加密文件
}
#基于客户端的访问控制 IP地址来进行控制
location / {
root html;
index index.html index.htm;
deny 192.168.39.40;
allow all;
}
#基于域名的nginx主机
server{
listen 80;
charset utf-8;
server_name www.luwei.com
access_log logs/www.luwei.com.access.log;
location / {
root /var/www/html/luwei;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
vim /etc/hosts
192.168.39.30 www.luwei.com
#添加本地hosts ip
#基于ip地址的虚拟主机
ifconfig ens33 192.168.39.100
server{
listen 192.168.39.100:80;
charset utf-8;
server_name www.luwei.com
access_log logs/www.luwei.com.access.log;
location / {
root /var/www/html/luwei;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#基于端口实现多个虚拟主机
server{
listen 192.168.39.30:8080;
charset utf-8;
server_name www.luwei.com
access_log logs/www.luwei.com.access.log;
location / {
root /var/www/html/luwei;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server{
listen 192.168.39.30:8888;
charset utf-8;
server_name www.luwei.com
access_log logs/www.luwei.com.access.log;
location / {
root /var/www/html/luwei;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#多个配置文件
vim nginx.conf
#在nginx配置中写入
include /usr/local/nginx/conf.d/*.conf; #可以识别到conf.d下,只包含server模块的conf文件
mkdir /usr/local/nginx/conf.d
vim test1.conf
server {
listen 8889;
server_name localhost;
location /test1 {
root /opt/conf/;
index index.html;
}
}
server {
listen 8890;
server_name localhost;
location /test2 {
root /opt/conf/;
index index.html;
}
}
mkdir -p /opt/conf/test1 opt/conf/test2
nginx优化
#nginx优化,隐藏版本号,防盗链
server_tokens off;
#设置页面的缓存时间,主要是针对动态页面,图片的缓存
location ~ \.(gif|jpg|png)$ {
root html;
expires 1d;
#设置图片的缓存时间
}
日志分割:
Apache是自带日志分割的,按天来进行收集日志
access.log
error-2024.07-03.log
#日志分割
#!/bin/bash
day=$(date -d "" "+%Y%m%d")
#先制定好日志,显示当天
logs_path="/usr/local/nginx/logs/"
pid_path="/usr/local/nginx/run/nginx.pid"
mv /usr/local/nginx/logs/access.log ${logs_path}access-${day}.log
mv /usr/local/nginx/logs/error.log ${logs_path}error-${day}.log
kill -USR1 $(cat ${pid_path})
#-USR1重新创建一个新的日志,日志分割
#超过30天的日志清理
find ${logs_path} -mtime +30 -exec rm -rf {} \;
#更改进程数已经设置cpu绑定
worker_processes 4;
#表示进程有4个,这里和cpu数挂钩,不绑定cpu的话,进程可能会在两个cpu之间来回切换使用。浪费资源
#绑定cpu,避免进程切换
worker_cpu_affinity 0001 0010 0100 1000;
#连接超时
keepalive_timeout 65;
#连接保持的时间,单位是秒
client_header_timeout 80;
#客户端发送一个完整的请求头的超时时间,80秒之内没有发送一个完整的请求头,nginx返回码408(request time ou)
client_body_timeout 80;
#客户端和服务端建立连接之后,发送请求体的超时时间,客户端在80秒内没有发送任何内容,nginx返回408
netstat -n | awk '/^tcp/ {++s[$NF]} END {for (a in s) print a s[a]}
统计当前系统的连接状态
回收TIME_WAIT:
time_wait是tcp链接当中的一种状态。出现四次挥手之后。
处于等待状态,双方不再发送数据
time_wait所占用的系统资源很小,数量比较少,完全可以忽略不计
但是太多,就有一定的影响
连接断开(四次挥手)之后,尽快的把time_wait状态的连接进行回收
#修改内核
vim /etc/sysctl.conf
net.ipv4.tcp_synvookies=1
#防止tcp的半连接队列溢出,可以达到服务端在收到tcp的syn(同步)的请求时能够快速响应
net.ipv4.tcp_tw_reuse=1
#允许复用time_wait状态的连接,新的连接可以直接使用time_wait状态的端口,可以提高连接的重用率
net.ipv4.tcp_tw_recycle=1
#这个是老版本的配置,时间戳戳记进行连接复用(已经淘汰)
net.ipv4.tcp_fin_timeout=30
#用来控制time_wait状态的持续时间,持续30秒,不是立即把time_wait的连接收回,而是尽可能的把time_wait状态回收,没用的空闲的>进行回收
~
防盗链
#图片防盗链,返回403
test1主机中
vim nginx.conf
location ~* \.(gif|jpg)$ {
valid_referers none blocked *.xy102.com xy102.com;
#允许xy102.com的网址访问图片
if ( $invalid_referer ) {
#rewrite ^/ http:www.xy102.com/error.png;
#如果不是xy102.com访问,一律跳转到倒链的提示
return 403;
}
}
网页准备:
Web源主机(192.168.233.61)配置:
cd /usr/local/nginx/html
将game.jpg、error.png文件传到/usr/local/nginx/html目录下
vim index.html
......
< img src="game.jpg"/>
</body>
</html>
echo "192.168.233.61 www.kgc.com" >> /etc/hosts
echo "192.168.233.62 www.benet.com" >> /etc/hosts
盗链网站主机(192.168.233.62):
cd /usr/local/nginx/html
vim index.html
......
< img src="http://www.kgc.com/game.jpg"/>
</body>
</html>
echo "192.168.233.61 www.kgc.com" >> /etc/hosts
echo "192.168.233.62 www.benet.com" >> /etc/hosts
在盗图网站主机上进行浏览器验证
http://www.benet.com
n
m
p
lnmp+DISCUZ
论坛的一个服务
l linux 操作服务
n nginx 前段页面的web服务
php 动态请求转发的中间件
mysql 数据库 保存用户和密码,以及论坛的相关内容
[client]
port = 3306
#客户端访问端口
socket=/usr/local/mysql/mysql.sock
#指定mysql的通信套接字文件
[mysqld]
user = mysql
basedir=/usr/local/mysql
#mysql的安装目录
datadir=/usr/local/mysql/data
#mysql数据保存的目录
port = 3306
#服务端的端口
character-set-server=utf8
#字符集的编码
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
#任意地址都可以访问数据库
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
#mysql默认的存储引擎。
max_allowed_packet=16M
server-id = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
#mysql支持的数据类型和相关的模块
php有三个配置文件
php.ini 主配置文件
php-fpm.conf 进程服务的配置文件
www.conf 扩展配置文件