我如何说服Nginxredirect除/robots.txt之外的所有请求?

我正在使用Nginx,并将一些旧的域名指向新的站点。

在这个configuration的第一个块工作正常,我需要old.domain行为时,redirectnew.domain

在第二个块中,我试图将任何除了 /robots.txt 之外oldmediaserver.domain请求转发到new.domain的主页。 在当前状态下,每个请求都会redirect,包括/robots.txt – 我不知道为什么。

(之所以这样做,是因为Google从旧域名中search了一些东西,我试图通过网站pipe理员工具从search结果中删除它 – 这可能行不通,但这不是我在这里要求的帮助! )。

# Old site to new site config server { listen 80; listen [::]:80; server_name old.domain www.old.domain; rewrite ^ $scheme://www.new.domain$request_uri permanent; } # Media server Redirect and Robots directive server { listen 80; listen [::]:80; server_name oldmediaserver.domain www.oldmediaserver.domain; location / { rewrite / $scheme://www.new.domain/ permanent; } location /robots.txt { return 200 "User-agent: *\nDisallow: /"; } rewrite ^ $scheme://www.new.domain/ permanent; } server { listen 80 default_server; listen [::]:80 default_server; root /var/www/website-name/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name www.new.domain; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.php?$args; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { include snippets/fastcgi-php.conf; # # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; } # include a file for any 301 redirects include includes/website-name-redirects; location /members/ { try_files $uri $uri/ /index.php?$args; auth_basic "Members Login"; auth_basic_user_file /var/www/website-name/html/.htpasswd; location ~ \.php$ { include snippets/fastcgi-php.conf; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; } } #!!! IMPORTANT !!! We need to hide the password file from prying eyes # This will deny access to any hidden file (beginning with a .period) location ~ /\. { deny all; } } 

谢谢你可以放弃任何光线!

感谢gf_和Drifter104的评论。 Drifter104的关于匹配位置的评论让我看到了不同的匹配模式,最终登陆到了下面的configuration。

 # Media server Redirect and Robots directive server { listen 80; listen [::]:80; server_name oldmediaserver.domain www.oldmediaserver.domain; location ^~ / { rewrite ^ $scheme://www.new.domain/ permanent; } location ^~ /robots.txt { return 200 "User-agent: *\nDisallow: /"; } } 

我还不确定我完全理解为什么这个工作,而另一个没有,所以如果任何人都可以摆脱更多的光线,这将是伟大的!