学完秒懂Nginx配置文件
Nginx访问状态统计概述
●Nginx内置了HTTP STUB STATUS状态统计模块,用来反馈当前的Web访问情况,
配置编译参数时可添加–with-http_stub_status_module来启用此模块支持,可以使用命令
/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS模块。
●要使用 Nginx 的状态统计功能, 除了启用内建模块以外, 还需要修改nginx.conf配置
文件, 指定访问位置并添加 stub_status 配置代码。
◆开启访问状态统计
●首先关闭你的防火墙和核心防护
[root@localhost~]# vi /usr/local/nginx/conf/nginx.conf
####省略#### 在localtion / 这个大模块下面 插入(添加)location /status 模块
location /{
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
}
####省略####
[root@localhost nginx]# nginx -t 检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# systemctl restart nginx
##打开浏览器去验证
Nginx访问控制
◆基于授权的访问控制
●基于授权的访问控制简介
Nginx与Apahce一样,可以实现基于用户授权的访问控制,当客户端想要访问相应网
站或者目录时,要求用户输入用户名和密码才能正常访问,配置步骤与Apache基本一致
●Nginx 网页认证实现步骤为:
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd.db zzz
New password:
Re-type new password:
Adding password for user zzz
[root@localhost ~]# chmod 400 /usr/local/nginx/.passwd.db #加个点表示隐藏文件
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# chown nginx .passwd.db ##更改属主,为了管理
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html; 去掉#
index index.html index.htm; 去掉#
auth_basic "secret"; #添加 #表示密码验证的方式
auth_basic_user_file /usr/local/nginx/.passwd.db; #认证的账号密码在什么地方
}
[root@localhost nginx]# systemctl restart nginx 配置完后正常重启
#打开电脑浏览器 http://20.0.0.26/
#输入账号:zzz
#输入密码:Abc123
#登录验证
◆基于客户端的访问控制
●配置规则:
规则从上往下执行,如匹配则停止,不再往下匹配
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
比如:除主机20.0.0.1之外允许其他客户端访问 可进行如下操作:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/.passwd.db;
deny 20.0.0.1;
allow all;
}
#######################添加的内容如下###########
deny 20.0.0.1;
allow all;
[root@localhost nginx]# systemctl restart nginx
###打开电脑浏览器访问 http://20.0.0.26/ ####发现访问不了了
Nginx虚拟主机概述
●利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一
组Nginx进程,虚拟主机提供了在同一台服务器,同一组 Nginx进程上运行多个网站的功
能。跟Apache一样,Nginx也可以配置多种类型的虚拟主机,分别是基于IP的虚拟主机、
基于域名的虚拟主机、基于端口的虚拟主机。
● 使用Nginx搭建虚拟主机服务器时,每个虚拟Web站点拥有独立的“serverf”配置段,
各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。
◆基于域名的虚拟Web主机
●修改windos客户机的C:\Windows\System32]drivers\etc\hosts文件,加入
www.51xit.top和www.52xit.top这两个域名,它们都指向同一个服务器IPР地址,用于实现
不同的域名访问不同的虚拟主机。
●为了方便验证
修改的配置文件如下 :20.0.0.26 www.51xit.top www.52xit.top
●或者再开一台Centos系统 修改/etc/hosts文件
修改的配置文件如下 :20.0.0.26 www.51xit.top www.52xit.top
◆第一步准备各个网站的目录和测试首页
[root@localhost~]# mkdir -p /var/www/html/51xit/ ####创建www.51xit.com的根目录
[root@localhost~]# mkdir -p /var/www/html/52xit/ ####创建www.52xit.com的根目录
[root@localhost~]# echo "www.51xit.top" >> /var/www/html/51xit/index.html
[root@localhost~]# echo "www.52xit.top" >> /var/www/html/52xit/index.html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ll
总用量 0
drwxr-xr-x. 2 root root 24 9月 3 16:07 51xit
drwxr-xr-x. 2 root root 24 9月 3 16:07 52xit
[root@localhost html]# cat 51xit/index.html
www.51xit.top
[root@localhost html]# cat 52xit/index.html
www.52xit.top
◆第二步修改配置文件,把配置文件中的server{代码段全部去掉,加入2个新的server{}
段,对应2个域名
###################首先将原来的server内容全部加#注释掉#############
# server {
# listen 80;
# server_name localhost;
#
# #access_log logs/host.access.log main;
#
# location / {
# root html;
# #auth_basic_user_file /usr/local/nginx/.passwd.db;
# #deny 20.0.0.1;
# #allow all;
# }
# location /status {
# stub_status on;
# access_log off;
# }
#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;
#}
# }
################################################################
####再在# another virtual host using mix of IP-, name-, and port-based configuration上空几行添加
##顶在最前面写,不留空格#
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log logs/www.51xit.top.access.log;
location / {
root /var/www/html/51xit;
index index.html index.htm;
}
error_page 500 502 503 504/50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.52xit.top;
charset utf-8;
access_log logs/www.52xit.top.access.log;
location / {
root /var/www/html/52xit;
index index.html index.htm;
}
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
#在这行上面添加!!!
###验证
开一台LINUX系统 做一下 /etc/hosts 映射
然后访问www.51xit.top 和 www.52xit.top
◆验证结果
◆基于IP的虚拟Web主机
●主机配置两个IP地址
20.0.0.26 192.168.100.26
◆第一步去VMware添加一个自定义 仅主机模式的网卡,然后配置网卡
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
ens33 35d9d64f-7719-42e9-b846-a1aeb356fa34 ethernet ens33
有线连接 1 ee2dccf4-cc4a-34bc-9cea-37e7d528cd27 ethernet ens36
##复制 ens36的 UUID :
ee2dccf4-cc4a-34bc-9cea-37e7d528cd27
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vi ifcfg-ens36
NAME=ens36
UUID=ee2dccf4-cc4a-34bc-9cea-37e7d528cd27
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.100.26
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
[root@localhost ~]# systemctl restart network
[root@localhost ~]# ifdown ens36
[root@localhost ~]# ifup ens36
#######打开电脑cmd ping一下 ping通继续
◆第二步修改配置文件
[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.26;
server_name 20.0.0.26:80;
......... ###省略部分
}
server {
listen 192.168.100.26;
server_name 192.168.100.26:80;
............ ###省略部分
}
#####就把刚刚上面基于域名的虚拟Web主机配置文件 开头换一下,换成你的网卡即可
####验证,用电脑浏览器访问,如果
20.0.0.26 出现 51xit.top
192.168.100.26 出现 52xit.top
即表示成功了
◆基于端口的虚拟Web主机
●只需要一个IP地址就够了
20.0.0.26
把刚刚配置的虚拟网卡删除再重启一下
●下面直接开始修改配置文件
[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 20.0.0.26;
server_name 20.0.0.26:6666;
......... ###省略部分
}
server {
listen 20.0.0.26;
server_name 20.0.0.26:8888;
......... ###省略部分
}
####验证,用电脑浏览器访问
http://20.0.0.26:6666/
http://20.0.0.26:8888/
Nginx配置文件详解
●讲解文件Nginx.conf
在Nginx服务器的主配置文件/usr/local/nginx/conf/nginx.conf中,包括全局配置、l/O
事件配置和 HTTP 配置这三大块内容,配置语句的格式为“关键字值;" (末尾以分号表示结
束) ,以“#"开始的部分表示注释.
##3.1全局配置##
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括Nginx服务的运行
用户、工作进程数、错误日志、PID存放位置等基本设置
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
#user nobody; ##运行用户
worker_processes 1; ##工作进程数
#error_log logs/error.log: ###错误日志文件的位置
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; ####PID文件的位置
●上述配置中, worker processes表示工作进程的数量。若服务器有多块CPU或者使用
多核处理器,可以参考CPU核心总数来指定工作进程数,如设为8;如果网站访问量需求并不大
,一般设为1就够用了。其他三项配置均已有注释,表示采用默认设置,例如, Nginx的运行用户
实际是编译时指定的nginx, 若编译时未指定则默认为 nobody
●l/O时间配置
使用"events ()"界定标记,用来指定Nginx进程的1/0响应模型、每个进程的连接数等设置。
对于2.6及以上版本的内核,建议使用 epoll 模型以提高性能 ; 每个进程的连接数应根据实际需
要来定, 一般在 10000 以下(默认为1024)
events {
use epoll;
worker_connections 4096;
}
若工作进程数为8,每个进程处理4096个连接,则允许Nginx正常提供服务的连接数已超过
3 万个 (4096x8-32 768) , 当然具体还要看服务器硬件、网络带宽等物理条件的性能表现。
●HTTP配置
使用"http {}"界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保
持,以及后面要讲到的虚拟Web主机、PHP解析等一系列设置,其中大部分配置语句都包含在
子界定标记"server {}"内
#user nobody;
worker_processes 1;
#error_log logs/error.log:
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
Worker_connections 4096;
}
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" "Shttp_x_forwarded_for"'; #去掉前面注释
access_log logs/access.log main; ###·访问日志位置,去掉前面注释
sendfile on; ###支持文件发送(下载)
#tcp_nopush on;
#keepalive timeout 0;keepalive timeout 65; ##链接保持时间
#gzip on;
server { ###WEB服务的监听配置
isten 80; ###WEB服务的监听端口
server_name www.51xit.top; ###网站名称(FQDN)
charset koi8-r; ###·网页的默认字符集
#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$ {
# proxy_pass http://127.0.0.1;
#}
#pass the PHP scripts to FastCGl 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;
#}
}
# 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
#
#servert {
# 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;
# }
#}
}
●上述配置中, listen语句允许同时限定 IP 地址, 采用 “IP 地址:端口” 形式; root语句用
来设置特定访问位置 ( 如 “location /” 表示根目录) 的网页文档路径, 默认为Nginx 安装
目录下的html/子目录, 根据需要可改为/var/www/html 等其他路径。