进入Nginx的主目录我们可以看到这些文件夹
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
其中这几个文件夹在刚安装后是没有的,主要用来存放运行过程中的临时文件
client_body_temp fastcgi_temp proxy_temp scgi_temp
worker_processes :worker_processes 1; 默认为1,表示开启一个业务进程
worker_connections:worker_connections 1024; 单个业务进程可接受连接数
include mime.types:include mime.types; 引入http mime类型
default_type application/octet-stream; default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
sendfile on; sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
未开启sendfile,文件先读取到nginx的进程内存,再发出
开启后,文件直接发出
keepalive_timeout 65;keepalive_timeout 65;
server {
listen 80; 监听端口号
server_name localhost; 主机名
location / { 匹配路径
root html; 文件根目录
index index.html index.htm; 默认页名称
}
error_page 500 502 503 504 /50x.html; 报错编码对应页面
location = /50x.html {
root html;
}
}
原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
server_name vod.mmban.com www1.mmban.com;
server_name *.mmban.com
server_name vod.*;
server_name ~^[0-9]+\.mmban\.com$;
proxy_pass http://baidu.com;
location / {
proxy_pass http://atguigu.com/;
}
upstream httpd {
server 192.168.44.102:80;
server 192.168.43.103:80;
}
upstream httpd {
server 127.0.0.1:8050 weight=10 down;
server 127.0.0.1:8060 weight=1;
server 127.0.0.1:8060 weight=1 backup;
}
配置反向代理来实现
location / {
proxy_pass http://127.0.0.1:8080;
root html;
index index.html index.htm;
}
location /css {
root /usr/local/nginx/static;
index index.html index.htm;
}
location /images {
root /usr/local/nginx/static;
index index.html index.htm;
}
location /js {
root /usr/local/nginx/static;
index index.html index.htm;
}
location ~*/(css|img|js) {
root /usr/local/nginx/static;
index index.html index.htm;
}
location /css {
alias /usr/local/nginx/static/css;
index index.html index.htm;
}
rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容,
重定向到replacement,结尾是flag标记。
rewrite [flag];
关键字 正则 替代内容 flag标记
关键字:其中关键字error_log不能改变
正则:perl兼容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记:rewrite支持的flag标记
rewrite参数的标签段位置:
server,location,if
flag标记说明:
last #本条规则匹配完成后,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
实例
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
upstream httpds {
server 192.168.44.102 weight=8 down;
server 192.168.44.103:8080 weight=2;
server 192.168.44.104:8080 weight=1 backup;
}
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 redirect;
proxy_pass http://httpds ;
}
systemctl start firewalld
systemctl restart firewalld
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.44.101" port protocol="tcp" port="8080" accept"
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.44.101" port port="8080" protocol="tcp" accept"
valid_referers none | blocked | server_names | strings ....;
valid_referers 192.168.44.101;
if ($invalid_referer) {
return 403;
}
适用curl测试
curl -I http://192.168.44.101/img/logo.png
带引用
curl -e "http://baidu.com" -I http://192.168.44.101/img/logo.png
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
安装依赖
yum install openssl-devel
yum install keepalived
使用yum安装后配置文件位置:/etc/keepalived/keepalived.conf
最小配置
第一台机器
! Configuration File for keepalived
global_defs {
router_id lb111
}
vrrp_instance atguigu {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.44.200
}
}
第二台机器
! Configuration File for keepalived
global_defs {
router_id lb110
}
vrrp_instance atguigu {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.44.200
}
}
启动服务
systemctl start keepalived
OpenSSL : 系统内置
图形化工具 XCA:下载地址