# 临时重定向
server{
listen 80;
server_name www.xhg.zone;
location / {
root html;
index index.html index.htm;
rewrite / http://www.xiehegang.online redirect;
}
}
# 永久重定向
server{
listen 80;
server_name www.xhg.zone;
location / {
root html;
index index.html index.htm;
rewrite / http://www.xiehegang.online permanent;
}
}
# 基于referer 实现防盗链
server{
listen 80;
server_name www.xhg.zone;
location{
root html;
index index.html index.htm;
valid_referers *.xiehegang.com *.xhg.zone www.baidu.* ; # 有效的访问
if ( $invalid_referer ) { #假如是使用其他的无效的referer访问:
return 403; #返回状态码403
}
}
}
# 编译的时候需要的额外模块
ngx_http_proxy_module
ngx_stream_proxy_module
ngx_http_fastcgi_module
ngx_http_uwsgi_module
#反向代理(七层)
upstream app{
server 192.168.47.122:80 ;
server 192.168.47.122:80 ;
}
server{
listen 80 ;
server_name www.xhg.zone;
location /web {
proxy_pass http://app/;
#如果配置后无法实现轮询则将工程名替换为web 或者将url 由web改成当前项目的工程名
}
}
# 缓存
#1 在nginx.conf 的http 中定义:
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=cache:20m;
# 2 server中定义:
server{
listen 80;
server_name www.xhg.zone;
location /web {
proxy_pass http://XXXXX:80; # 负载地址
proxy_cache cache; #一定要是keys_zone的值
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m; #指定的状态码返回的数据缓存多长时间
proxy_cache_valid any 1m;
}
}
#负载均衡mysql/redis(四层)
stream {
upstream redis_server {
server 192.168.7.104:6379 max_fails=3 fail_timeout=30s;
}
upstream mysql_server {
least_conn;
server 192.168.7.104:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 192.168.7.102:3306;
proxy_connect_timeout 6s;
proxy_timeout 15s;
proxy_pass mysql_server;
}
server {
listen 192.168.7.102:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
# include 文件的时候,需要放在与http同级
#include xxx.conf;
#http{
# ...
#}
原文:https://www.cnblogs.com/Justin0717/p/12178292.html