Nginx配置
Nginx原配置文件
#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 {
worker_connections 1024; #
}
http {
include mime.types; # 引入http mime类型
default_type application/octet-stream; # 如果mime类型没匹配上,默认使用二进制流的方式传输。
#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;
sendfile on; # 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#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 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 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# 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;
# }
#}
}
sendfile on
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ou3mmQ3T-1667977744779)(https://dinosaurimgs.oss-cn-hangzhou.aliyuncs.com/csdnblog/202211041640825.png)]
sendfile off
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3uqAIQlR-1667977744780)(https://dinosaurimgs.oss-cn-hangzhou.aliyuncs.com/csdnblog/202211041641444.png)]
虚拟机配置
# 虚拟机配置
server {
listen 80; # 监听的端口号
server_name localhost; # 配置域名或者主机,例如www.dinsoaurhou.cn,可以配置多个域名
# uri匹配
location / { # 匹配路径
root html; # 文件根目录,可以进行修改
index index.html index.htm; # 默认页面名称
}
error_page 500 502 503 504 /50x.html; # 报错编码对应页面
location = /50x.html {
root html;
}
}
域名解析
在阿里云或者腾讯云购买一个域名,因为在弄自己的Halo
博客的时候,买过一个域名dinosaurhou.cn
,然后可以使用域名DNS
解析,让域名绑定在我的内网ip
地址上(192.168.45.128
),然后等待解析可以发现我们就可以访问Nginx
的主页
Nginx配置使用场景
修改配置文件之后可以重新加载Nginx
systemctl reload nginx
Nginx反向代理
在虚拟机配置中可以修改server
下的location / {}
添加属性proxy_pass
至其他主机地址,反向代理至其他服务器。
Nginx负载均衡
upstream httpds {
server 192.168.45.129:80 weight=8 down; # 可以填写多个地址进行负载均衡
server 192.168.45.130:80 weight=2; # 负载均衡策略:weight
server 192.168.45.131:80 backup;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://httpds;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
负载均衡策略weight、down、backup
weight
:可以为不同的代理服务器设置权重
down
:可以让其中的代理服务器不使用
backup
:备用机,正常使用情况下不使用此服务器
ip_hash
:根据客户端的ip
地址转发同一台服务器,而可以保持会话
least_conn
:最少连接访问
url_hash
:根据用户访问的url
定向转发请求
fair
:根据后端服务器响应时间转发请求
Nginx动静分离
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.45.129:8080; # 反向代理
}
location /css {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
正则表达式动静分离
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.45.129:8080; # 反向代理
}
location ~*/(css|img|js) {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
URL rewrite
server {
listen 80;
server_name localhost;
location / {
rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
proxy_pass http://192.168.45.129:8080; # 反向代理
}
location ~*/(css|img|js) {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
rewrite是URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结尼是flag标记。
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flagt标记
正则:per1森容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记说明:
last #本条规则匹配完成后,继续向下匹配新的1ocation URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临重定向,游览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,测览器地址栏会显示跳转后的URL地址
Centos 防火墙配置
说明:开启或者关闭指定端口后,需要重启防火墙才能生效
systemctl status firewalld # 防火墙状态
systemctl start firewalld # 防火墙开启
systemctl restart firewalld # 防火墙开启
systemctl stop firewalld # 关闭防火墙
firewall-cmd --permanent --zone=public --add-port=27017/tcp --permanent # 开启指定端口
firewall-cmd --zone=public --remove-port=27017/tcp --permanent # 关闭指定端口
firewall-cmd --permanent --zone=public --add-port=27017-30000/tcp --permanent # 开启指定范围端口
firewall-cmd --permanent --zone=public --add-port=1-65535/tcp --permanent # 开始所有端口
firewall-cmd --zone=public --remove-port=27017-30000/tcp --permanent # 关闭指定范围端口
firewall-cmd --permanent --query-port=27017/tcp # 查看端口是否开放
firewall-cmd --reload # 重启防火墙
firewall-cmd --list-ports # 查看已经开放的端口
iptables -L -n # 查看规则,这个命令是和iptables的相同的
man firewall-cmd # 查看帮助
参数说明
--zone # 作用域
--add-port=80/tcp # 添加端口,格式为:端口/通讯协议
--remove-port=80/tcp # 移除端口,格式为:端口/通讯协议
--permanent # 永久生效,没有此参数重启后失效
添加富规则允许192.168.45.128
访问8080
端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.45.128" port protocol="tcp" port="8080" accept"
移除富规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.45.128" port protocol="tcp" port="8080" accept"
Nginx防盗链
server {
listen 80;
server_name localhost;
location / {
rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
proxy_pass http://192.168.45.129:8080; # 反向代理
}
location ~*/(css|img|js) {
valid_referers none 192.168.45.128;
if ($invalid_referer) {
return 403;
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
none
:检测Referer
头域不存在的情况blocked
:检测Referer
头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值以http://
或https://
开头server_names
:设置一个或多个URL
,检测Referer
头域的值是否是这些URL
中的某一个
跳转错误页面
server {
listen 80;
server_name localhost;
location / {
rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
proxy_pass http://192.168.45.129:8080; # 反向代理
}
location ~*/(css|img|js) {
valid_referers none 192.168.45.128;
if ($invalid_referer) {
# return 403;
rewirte ^/ /img/x.png break; # 返回防盗图片
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
error_page 401 /401.html; # 错误请求
location = /401.html {
root html;
}
}
结语
经过两周的学习,Nginx
算是基本了解了,至少Halo
文档中相关的Nginx
配置是基本能看懂一点了,会自己配置一点了。了解了什么是负载均衡,什么是反向代理,什么是动静分离,什么是防盗链,还了解了一些Centos
的相关操作,以及防火墙的一些命令等。但是我觉得让我最受益的是对计算机网络的一些知识更加了解了,至少对其有一个更加清晰的认识,而不只是局限于书本。
但是我个人感觉自己的学习效率还是不够高的,希望我自己可以继续加把劲!🛩