server {
listen 80;
server_name old-domain.com;
return 301 https://new-domain.com$request_uri;
}
server {
listen 80;
server_name temp-domain.com;
return 302 https://main-domain.com$request_uri;
}
location /old-path/ {
return 301 https://$host/new-path$request_uri;
}
location ~* ^/products/([0-9]+) {
return 301 /items/$1;
}
server {
listen 80;
server_name www.domain.com domain.com;
if ($host != 'www.domain.com') {
return 301 https://www.domain.com$request_uri;
}
# 其他配置...
}
server {
listen 80;
server_name sub.old.com;
return 301 https://sub.new.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
# SSL配置...
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
location /api/ {
proxy_pass http://backend-server:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
location /download/ {
rewrite ^/download/(.*)$ /files/$1 break;
}
if ($arg_param = "redirect") {
return 301 https://new-site.com;
}
if ($http_user_agent ~* "(bot|crawler|spider)") {
return 301 https://crawler.example.com$request_uri;
}
重定向循环问题:
curl -v
或浏览器开发者工具跟踪重定向链保留原始URL参数:
$request_uri
而非$uri
,因为前者包含查询参数性能优化:
SEO考虑:
nginx -t
nginx -s reload
curl -I http://example.com/old-path
tail -f /var/log/nginx/access.log
通过合理配置这些重定向规则,您可以实现灵活的URL管理、流量分发和爬虫控制,同时确保良好的用户体验和SEO效果。