【问题标题】:Robots.txt should not be redirected to HTTPSRobots.txt 不应重定向到 HTTPS
【发布时间】:2019-11-19 16:24:22
【问题描述】:

我有以下 Nginx 配置。我将所有 HTTP 请求重定向到 HTTPS。我想要实现的是 robots.txt(“http://example.com/robots.txt”或“http://example2.com/robots.txt”)上的 HTTP 请求不会被重定向到 HTTPS。我很难找到合适的陈述。

我的 Nginx 配置

server {

    server_name  example.com  www.example.com  example2.com  www.example2.com;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_set_header X-HTTPS-Protocol $ssl_protocol;
    proxy_set_header X-NginX-Proxy true;

    location / {
        proxy_pass http://127.0.0.1:9092;
    }

    listen 443 ssl;
    ssl_certificate /path
    ssl_certificate_key /path
    include /path
    ssl_dhparam /path



}
server {

    server_name  example.com  www.example.com  example2.com  www.example2.com;

    listen 80;
    listen [::]:80;

    #
    # What to put here not to redirect robots.txt to HTTPS?
    #

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    }

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    }

    if ($host = www.example2.com) {
        return 301 https://$host$request_uri;
    }

    if ($host = example2.com) {
        return 301 https://$host$request_uri;
    }

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_set_header X-HTTPS-Protocol $ssl_protocol;
    proxy_set_header X-NginX-Proxy true;

    location / {
        proxy_pass http://127.0.0.1:9092;
    }

    return 404;
}

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

    简单的答案是废弃第二个server 块并重新开始。也许是这样的:

    server {
        server_name  example.com  www.example.com  example2.com  www.example2.com;
    
        listen 80;
        listen [::]:80;
    
        location / {
            return 301 https://$host$request_uri;
        }
        location = /robots.txt {
            root /path/to/directory;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2015-08-07
      • 2011-08-18
      • 1970-01-01
      • 2014-07-05
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多