nginx.conf
1.全局模块
user nobody;
#默认的程序用户就是nginx,这里可以保存注释无需修改,如果取消注释,需将user改成nginx
worker_processes 1
#工作进程数,一般设置成服务器内核的2倍(一般不超过8个,超过8个反而会降低性能)
pid /usr/local/nginx/run/nginx.pid
#pid文件的位置
events {
worker_connections 1024;
}
#events模块,觉得了nginx能够处理的连接数,连接数和worke_processes的数值相乘
#处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量
#linux默认打开的文件数就是1024个
vim /etc/security/limits.conf
* soft nproc 65535 #能打开的进程最大数的软限制是65535,65535是最大数
* hard nproc 65535 #硬限制
* soft nofile #进程打开文件数的最大值65535
* hard nofile 65535 #硬限制
#这个配置要生效只能重启,这是系统初始化的一个环节
#http转发和处理http请求,设置代理(正向代理和反向代理),缓存,定义日志格式,重定向配置。
include mime.types;
#文件扩展名与文件类型的映射表。nginx能够打开的文件和支持的文件类型
default_type application/octet-stream;
#默认支持的文件类型 .html .htm .jsp .php .js
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 65;
#连接保持的时间65s,单位是秒
gzip on;
#gzip模块,设置是否开启页面压缩的功能(没啥用)
server { #开启web服务模块
listen 80; #nginx的默认监听端口
server_name localhost; #配置站点的域名
charset koi8-r; #网页的默认字符集 utf-8换成中文
access_log #logs/host.access.log main;
location /x { #网页匹配的工作目录地址和支持的打开网页的文件类型
root html;
#root表示家目录nginx工作目录的家目录 /usr/local/nginx/html
#root的匹配模式是拼接 /html/x
alias /opt/testq/xxx
#alias也是指匹配nginx的工作目录,alias匹配模式是绝对路径
#alias只能写在http模块当中server模块的location模块里面
#root可以写在server模块,也可以在http,也可以在location
#使用alias匹配工作目录帮使用重定向功能
index index.html index.htm;
#index.js
}
全局模块
worker_processes 1; 指定进程数
events模块决定了能够处理的连接数
stream 四层代理模块
http模块
转发和处理http请求,设置代理(正向代理和反向代理),缓存,定义日志格式,重定向配置。
在http模块当中,包含:
server块 http里面可以有多个server模块
在server模块中包含:
location模块
在server当中可以有多个location
location /status {
stub_status on; #打开状态统计的功能;
access_log off; #关闭status的访间日志
}
Active connections:1 #当前活动的连接数是1
server accepts handled requests #表示已经处理的连接数
36 36 36 #从左往右1.已经处理的连接数 2.超过建立连接的次数3.已经处理的连接数
Reading:o Writing:l waiting:0
Reading #表示服务端正在从客户端读取请求的数据
Writing #表示服务端正在把响应数据发送给客户端
waiting #表示有连接处于空闲状态,等待新的请求
#基于密码的授权进行访问控制
yum -y install httpd-tools #httppasswd的工具,要先安装
htpasswd -c /usr/local/nginx/passwd.db 用户名
123
123
cd /usr/local/nginx
ls
chown nginx passwd.db
chmod 400 passwd.db
vim conf/nginx.conf
location /xy103 {
root /opt/test1;
index index.html;
auth_basic "secret";
#开启用户密码验证
auth_basic_user_file /usr/local/nginx/passwd.db;
#使用指定的加密文件
}
#基于客户端的访问控制 IP地址来进行控制
vim /usr/local/nginx/conf/nginx.conf
#添加一个控制规则
location / {
root /optio/test1;
index index.html;
deny 192.168.233.10;
allow all;
}
#基于域名的nginx主机
vim /usr/local/nginx/conf/nginx.conf
#配置站点的域名
server_name www.gjw.com;
#设置访问日志的存放路径
access_log logs/www.gjw.com.access.log;
root /var/www/html/gjw;
vim /etc/hosts
#把地址和域名做一个映射,访问域名即访问这个IP地址
192.168.233.10 www.gjw.com
#基于IP地址的虚拟主机
ifconfig ens33:0 192.168.233.100/24
vim /usr/local/nginx/conf/nginx.conf
listen 192.168.233.10:80;
listen 192.168.233.100:80;
vim /usr/local/nginx/conf/nginx.conf
#基于端口实现多个虚拟主机
listen 192.168.233.10:8080;
listen 192.168.233.10:8081;
#多个配置文件
#可以识别到conf.d下,只包含server模块的conf文件
include /usr/local/nginx/conf.d/*.conf;
mkdir conf.d
cd conf.d/
vim test1.conf
server {
listen 8888;
server_name localhost;
location /test1 {
root /opt/conf;
index index.html;
}
}
server {
listen 8880;
server_name localhost;
location /test2 {
root /opt/conf;
index index.html;
}
}
nginx -t
systemctl restart nginx.service
cd /opt
mkdir -p conf/test1
mkdir -p conf/test2
ls
cd conf
ls
echo "this is test1" > test1/index.html
echo "this is test2" > test2/index.html
cd /usr/local/nginx/conf.d/
ls
netstat -anpt | grep nginx