【问题标题】:Nginx configuration to add www neglects request uriNginx 配置添加 www 忽略请求 uri
【发布时间】:2014-01-18 08:31:52
【问题描述】:

以下是我在 nginx.conf 中使用的配置,用于重写所有非 www.example.com 请求以添加 www。

server {
    listen       80 default_server;
    server_name  example.co.uk;
    rewrite ^(.*) http://www.example.co.uk$1 permanent;
}

server {
  listen 80;
  server_name www.example.co.uk;
  root /home/ubuntu/apps/example/current/public;
  ...
}

期望: http://example.com/image4.png 应该永久重定向到http://www.example.com/image4.png

但是当http://example.com/image4.png 请求时,它会重定向到http://example.com

我在哪里做错了?

【问题讨论】:

    标签: linux http redirect nginx


    【解决方案1】:

    我真的不知道出了什么问题,但可能$1 是空的,这是一个更好的方法。

    server {
        listen 80 default_server;
        server_name  example.co.uk;
        return 301 http://www.example.co.uk$request_uri;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 2020-04-04
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2019-02-12
      • 2011-01-16
      • 1970-01-01
      相关资源
      最近更新 更多