1.Nginx 安装和启动
准备:
yum repolist 查看当前yum源的可下载软件数量
yum install epel-rleae -y 安装yum的扩展包,增加软件库软件数量
yum install nginx -y 安装nginx
启动nginx: systemctl start nginx
2.Nginx 配置
nginx.conf nginx 配置文件
注意:grep -Ev '#|^$' nginx.conf.default > nginx.conf 先将nginx.conf配置文件的空格和注释去掉
worker_process 1 工作进程(受管理进程master process控制,最大不超过cpu 核数)
worker_connections 1024 最大处理请求数
include mine.types nginx 支持文件类型
default_type 超出 mine.types无法识别的默认打开方式
charset utf-8 ; #设定nginx 字符集
server {
listen 80; nginx 默认端口
server_name 配置域名
location / { 指定网站根目录
root 根目录指定 # /web01 根目录下wen01 web01 相对路径 /usr/share/nginx 下的web01
index index.html index.htm;
}
}
Nginx 多端口部署多站点配置
Nginx 多ip部署多站点
linux: 找到网卡配置文件,添加
nginx 配置如下:
效果如下:
Nginx 多域名部署多站点
nginx 配置三级域名:
include 配置引入:
注意:listen default_server 多域名时 默认访问的网站
Nginx 日志记录
日志位置:/var/log/nginx/
access.log
error.log
定制日志格式: log_fomat 日志记录名 设置规则
$remote_addr 客户端ip
$remote_user 客户端用户名
$time_local 当前时间
$request 请求起始行
$status http状态码
$bytes_sent 响应资源大小
$http_referer 记录资源的跳转地址
$http_user_agent 用户的终端信息
$gzip_ratio gzip的压缩级别
例如:
log_format compression '$remote_addr - $remote_user[$time_local]' '$request $status $bytes_sent' #设置在配置文件的server 外面
网站单独设定日志:
server {
access_log 位置 日志记录名
}
Basic 认证
1.生成一个htpasswd 的账号文件
2. 新建编辑htpasswd文件
guojia:NAt.lkSTt37u2
3.修改配置文件nginx.conf
location / {
auth_basic 描述信息
auth_basic_user_file 路径/htpasswd
}
4.添加效果
Nginx ssl证书配置
server {
#listen 443 ssl;
listen 80;
server_name www.wulaoban.top;
#ssl_certificate /usr/share/nginx/cert/9683539_wulaoban.top.pem;
#ssl_certificate_key /usr/share/nginx/cert/9683539_wulaoban.top.key;
#ssl_session_timeout 5m; # 超时时间
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #请按照以下协议配置
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
#ssl_prefer_server_ciphers on;
location / {
root webPro/web01/yiliao;
index index.html index.htm;
}
}
实际效果:
Nginx return跳转
效果:
Nginx rewrite 跳转
额外配置:
server {
listen 80;
server wulaoban.top
location / {
return 302 https://www.wulaoban.top$request_uri;
}
}
wulaoban.top 和www.wulaoban.top 都可以访问到 https://www.wulaoban.top
Nginx 的gzip压缩配置
好处:节省客户端流量
参数:
gzip on;//压缩开关
gzip_buffers 4 32K ; //内存缓冲
gzip_comp_level 9; // 压缩等级
gzip_min_length 1k; //最小压缩文件,低于1k不压缩
gzip_http_version 1.1; //http版本压缩版本
gzip_types application/javascript text/css text/xml; //压缩文件类型
gzip_disable "MSIE [1-7]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_vary on; //http响应头添加gzip 标识
Nginx 目录浏览
需要删除目录下的 index 文件,才能查看到
参数:
server {
autoindex on;#开启目录浏览功能
autoindex_exact_size off;#显示文件带单位
}
Nginx 访问控制(应用层)
加黑 deny 加白 allow
参数设置:
server {
location / {
allow 0.0.0.0/0 ; //允许所有访问
deny 192.168.1.110 ; //禁止192.168.1.110 访问
}
}
其他手段禁用访问:
防火墙设置禁用ip(基于传输层)
systemctl start firewalld // 开启防火墙
firewall-cmd --add-rich-rule='rule family=ipv4 source address="192.168.1.110" drop' //添加郭泽,禁止192.168.1.110 访问该主机
Nginx Location 优先级
location 可以有多个
单独给a目录加basic 认证
~ 匹配正则 区分大小写
~* 不区分大小写 匹配正则
= 针对文件 精准匹配不支持正则
访问任意txt 结尾文件/文件夹 返回404
location ~* ^.*\.txt {
return 404;
}
访问a开头的路径禁止访问
location ~ /a+/ {
return 404;
}
Nginx常用变量
$host //http请求头
$referer //从哪一个url跳转过来的
$user_agent //用户的浏览器客户端信息
$connection //是否为长连接
$remote_addr //客户端的ip
$status //http状态码
Nginx 防盗链
记录本次访问url 的来源
防盗网站配置文件:
location ~* \.png$ {
if($http_referer !~* "a.jaden.com"){ # !~* 不包含意思
return 403;
}
}
Nginx 中英文自动匹配
根据浏览器设置的语言提供选择
浏览器的accept_language
Nginx 搭建php动态语言
安装php-fpm:
yum install php-fpm php-mbstring php-mysqlnd php-gd -y
修改配置/etc/php-fpm.d/www.conf :
user = nginx //39行
group = nginx //41行
重启服务:
systemctl start php-fpm
systemctl enable php-fpm
查看启动端口:
netstat -ntlp //会有一个9000 端口
修改nginx 配置文件,修改对应网站的配置:
server {
location / {
root /html/wulaoban;
index index.php index.html index.htm;
}
location ~\.php$ {
root /html/wulaoban; //也可以放到全局变量
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /html/wulaoban$fastcgi_script_name;
include fastcgi_params;
}
}
新建网站目录:mkdir /html/wulaoban
下载可道云 个人部署包到该目录 ,解压,修改目录文件夹权限:
unzip 压缩包
chown -R nginx:nginx 目录
重启nginx:
systemctl restart nginx;