Nginx
配置文件
每次改完配置文件都要重启服务
启动服务:start nginx
停止服务:nginx -s stop
重新加载:nginx -s reload(配置文件被修改后需要执行它)
重定向
Rewrite指令
link <-- 这一篇还有if条件和防盗链,写得超级好
rewrite ^/(.*) http://www.baidu.com/$1 permanent;
说明:
rewrite 为固定关键字,表示开始进行rewrite匹配规则。
regex 为 ^/(.*)。 这是一个正则表达式,匹配完整的域名和后面的路径地址。
replacement就是 http://www.baidu.com/1这块了,其中1这块了,其中1是取regex部分()里面的内容。如果匹配成功后跳转到的URL。
flag 就是 permanent,代表永久重定向的含义,即跳转到 http://www.baidu.com/$1 地址上。
server {
listen 8088;
server_name xxx.abc.com;
location / {
proxy_pass http://127.0.0.1:3001;
rewrite ^/(.*) http://www.baidu.com permanent;
}
}
监听端口号是8088,然后server_name 配置设置为 xxx.abc.com, 然后当我们访问 http://xxx.abc.com:8088/的时候,会先反向代理到我们的http://127.0.0.1:3001下的node对应的页面上来,反向代理完成后,会使用 rewrite 重定向百度页面去了。
对 http://xxx.abc.com:8088/ 进行永久重定向(301), 然后会访问百度(307)临时重定向,最终加载百度页面的地址(200)
反向代理
//nginx.conf中
location /aaa {
proxy_pass http://192.168.1.60:80;
}
比如请求127.0.0.1/aaa/index,会自动转到http://192.168.1.60:80/aaa/index
location /aaa/ {
proxy_pass http://192.168.1.60:80/;
}
如果在两个后面都加上/
那127.0.0.1/aaa/index会转向http://192.168.1.60:80/index
负载均衡
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream wgf.com{
server 192.168.1.98; #真实服务器A(sxwgf-PC)
server 192.168.1.99; #真实服务器B(hzdk-vpc)
}
server { #Nginx代理服务器
listen 8088;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://wgf.com;
proxy_redirect default;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
访问127.0.0.1:8088会随机访问到192.168.1.98和192.168.1.99