目录标题
Nginx
-
nginx -t
查看配置文件是否配置正确 -
cat /usr/local/nginx/logs/nginx.pid
查看Nginx的PID号
netstat -natp | grep nginx
ss -tnlp | grep 80
ps -ef | grep nginx
-
kill -3 <PID号>
获取到进程号可以通过kill杀掉进程关闭服务
kill -s QUIT <PID号>
killall -3 nginx
killall慎用可能会将别的相关的进程一起杀掉
killall -s QUIT nginx
-
kill -1 <PID号>
重载
kill -s HUP <PID号>
killall -1 nginx
-
kill -USR1 <PID号>
日志分割,重新打开日志文件 -
kill -USR2 <PID号>
平滑升级
通过写脚本来添加Nginx系统服务
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
-:代表不设置自启动,99:代表第99个顺序启动服务,20:代表第20个关闭服务
#thsi is nginx serve
脚本介绍
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
以上脚本内容
chmod +x /etc/init.d/nginx
给脚本加权限
chkconfig --add nginx
添加为系统服务
service nginx start
启动服务
service nginx stop
关闭服务
注1:使用脚本的方法启动服务会慢一点可能要30S-60S
注2:使用此脚本启动后不能与systemctl stop nginx命令串用
认识Nginx服务的主配置文件nginx.conf
vim /usr/local/nginx/conf/nginx.conf
- 全局配置
#user nobody;
运行用户,若编译时未指定则默认为 nobody
worker_processes 1;
工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了(cat /proc/cpuinfo | grep processor | wc -l 查看CPU核数)
error_log logs/error.log;
错误日志文件的位置
pid logs/nginx.pid;
PID 文件的位置 - I/O 事件配置
events {
use epoll;
使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 4096;
每个进程处理 4096 个连接两核则2乘以4096
}
如提高每个进程的连接数还需执行ulimit -n 65535
命令临时修改本地每个进程可以同时打开的最大文件数。
在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
可使用ulimit -a
命令查看系统允许当前用户进程打开的文件数限制。
- HTTP 配置
http {
include mime.types;
文件扩展名与文件类型映射表在同目录中映射的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;
访问日志位置main表示主,只能有一个地方在http配置项当中并且在server项外
sendfile on;
支持文件发送(下载)
#tcp_nopush on;
此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#keepalive_timeout 0;
连接保持超时时间,单位是秒
keepalive_timeout 65;
一般服务器为60秒左右
#gzip on;
gzip模块设置,设置是否开启gzip压缩输出
server {
Web 服务的监听配置
listen 80;
监听地址及端口,可加ip地址或端口号
server_name www.chen.com;
站点域名,可以有多个,用空格隔开
charset utf-8;
网页的默认字符集
location / {
根目录配置yum安装的在/var/www/html,源码编译安装在/usr/local/nginx/html
root html;
网站根目录的位置/usr/local/nginx/html
index index.html index.php;
默认首页文件名
}
error_page 500 502 503 504 /50x.html;
内部错误的反馈页面
location = /50x.html {
错误页面配置
root html;
}
}
}
日志格式设定:
$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(根路径配置):请求www.chen.com/test/1.jpg
,会返回文件/usr/local/nginx/html/test/1.jpg
alias(别名配置):请求www.chen.com/test/1.jpg
,会返回文件/usr/local/nginx/html/1.jpg
proxy_pass(反向代理)
proxy_pass http://127.0.0.1:8080/
会转发请求到http//:127.0.0.1:8080/1.jpg
proxy_pass http://127.0.0.1:8080
会转发请求到http//:127.0.0.1:8080/test/1.jpg
访问状态统计配置
- 先使用命令
/usr/local/nginx/sbin/nginx -V
查看已安装的Nginx是否包含 HTTP_STUB_STATUS 模块 - 修改 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.chen.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
location /status {
添加stub status配置 访问位置为/status
stub_status on;
打开状态统计功能
access log off;
关闭此位置的日志记录
}
}
}
- 重启服务。访问测试
systemctl restart nginx
浏览器访问http://192.168.150.10/status
Active connections :表示当前的活动连接数
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、已处理的请求数
基于授权的访问控制
-
生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/userlist.pd chen
htpasswd /usr/local/nginx/userlist.pd wang
cat /usr/local/nginx/userlist.pd
chmod 600 /usr/local/nginx/userlist.pd
chown nginx userlist.pd
mv index.html index.html.bak
vim index.html
-
修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
auth_basic "hello boy!!";
auth_basic_user_file /usr/local/nginx/userlist.pd
http://192.168.150.10
基于客户端的访问控制
- 访问控制规则
deny :拒绝某个IP或IP段的客户端访问
allow :允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
vim /usr/local/nginx/nginx.conf
......
server {
location / {
......
deny 192.168.150.20;
拒绝访问客户端的IP
allow all;
允许其它IP客户端访问
}
}
systemctl restart nginx
基于域名的 Nginx 虚拟主机
- 为虚拟主机提供域名解析
echo "192.168.150.10 www.chen.com www.wang.com" >> /etc/hosts
或者可以做个DNS解析
- 为虚拟主机准备网页文档
mkdir -p /var/www/html/chen
mkdir -p /var/www/html/wang
echo "<h1>www.chen.com</h1>" > /var/www/html/chen/index.html
echo "<h1>www.wang.com</h1>" > /var/www/html/wang/index.html
- 修改 Nginx 的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name www.chen.com;
设置域名www.chen.com
charset utf-8;
access_log logs/www.chen.access.log;
设置日志名
location / {
root /var/www/html/chen;
上设置www.chen.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.wang.com;
charset utf-8;
access_log logs/www.wang.access.log;
location / {
root /var/www/html/wang;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html {
root html;
}
}
www.chen.com
www.wang.com打开浏览器验证
基于IP的 Nginx 虚拟主机
ifconfig ens33:0 192.168.150.11/24
添加一张虚拟网卡
如果有两张物理网卡则不要设置同一个网段会打环
可在基于域名的基础上进行修改
liisten 192.168.150.10:80;
listen 192.168.150.11:80;
通过不同的IP地址访问对应的域名
192.168.150.10
访问验证——>www.chen.com
192.168.150.11
访问验证——>www.wang.com
基于端口的 Nginx 虚拟主机
可以在基于域名IP的前提上继续修改
liisten 192.168.150.10:8080;
通过端口进行访问
liisten 192.168.150.11:8070;
通过端口进行访问
通过不通同的端口访问对应的域名
192.168.150.10:8080
访问验证——>www.chen.com
192.168.150.11:8070
访问验证——>www.wang.com