linux - Nginx 配置添加 www 忽略请求 uri

标签 linux http redirect nginx

以下是我在 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 .

我哪里做错了?

最佳答案

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

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

关于linux - Nginx 配置添加 www 忽略请求 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21201870/

相关文章:

linux - 如何理解内核中 "page_align"的宏?

linux - iptables:什么是链?

Apache 无法从外部访问,在内部工作

javascript - 如何调动所有 http ://www. google.com/gwt/x?u=? 的链接

http - 相同的页面,不同的网址。重定向?

linux - 如何使用 perl 更改邮件探测中的 ip

linux - 15 分钟后将更新的文件从一台服务器复制到另一台服务器

json - 如何使用 VBS 发布漂亮的 JSON

http://vs http://www:我应该使用子域还是永久重定向

apache - 将所有流量重定向到 https ://www. 目前我有两个版本 http://www 和 https://www