【问题标题】:Nginx non-www not working and redirect in Ubuntu 16.04Nginx 非 www 无法在 Ubuntu 16.04 中工作和重定向
【发布时间】:2017-05-15 10:42:02
【问题描述】:

我的服务器在 DO 中运行在 Ubuntu 16.04 中。 我的问题是,当我尝试在没有 www 的情况下运行时,我的网站无法加载。

我从这里开始学习教程。 https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

我的目标是

  1. 将所有非www重定向到https://example.com
  2. 将所有www重定向到https://www.example.com

nginx/sites-available/nginx_config

server {
    listen 80;
    server_name example.com http://example.com;
    return 301 https://example.com$request_uri;
}
server {
        listen 80;
        server_name www.example.com http://www.example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;
        root /home/tim/site.folder;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location ~ /.well-known {
            allow all;
        }
        location / {
            try_files $uri $uri/ =404;
        }
}

到目前为止我所尝试的:

删除 第一个和第二个服务器块中的 http。 (在第二个服务器块中相同)

server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

【问题讨论】:

    标签: ubuntu nginx


    【解决方案1】:

    实现你的目标
    1.将所有非www重定向到https://example.com 2.将所有www重定向到https://www.example.com 这可以通过

    server_name www.example.com example.com;
    

    你可以像下面这样使用完整的配置

    server {
      listen 80;
      server_name www.example.com example.com;
      access_log off;
      index index.html index.htm index.php;
      listen 443 ssl http2 default_server;
      listen [::]:443 ssl http2 default_server;
      include snippets/ssl-example.com.conf;
      include snippets/ssl-params.conf;
      root /home/tim/site.folder;
    
    location ~ [^/]\.php(/|$) {
     #fastcgi_pass remote_php_ip:9000;
     fastcgi_pass unix:/dev/shm/php-cgi.sock;
     fastcgi_index index.php;
     include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
     expires 30d;
     access_log off;
    }
    location ~ .*\.(js|css)?$ {
     expires 7d;
     access_log off;
    }
    }
    

    【讨论】:

    • 感谢您的回答。打算试试这个。
    【解决方案2】:

    不要放http

     server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
    }
    
     server {
    listen 80;
    server_name www.example.com;
    return 301 https://www.example.com$request_uri;
    }
    
     server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name example.com www.example.com;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;
        root /home/tim/site.folder;
        index index.html index.htm index.nginx-debian.html;
    
        location ~ /.well-known {
            allow all;
        }
        location / {
            try_files $uri $uri/ =404;
        }
     }
    

    【讨论】:

    • 请查看更新后的问题。我包括了到目前为止我尝试过的内容。感谢回复
    • 哪个服务器阻塞?我在任何 server_name 下都看不到 http。
    • 对不起,我现在看到你更新的代码了。您的服务器块现在应该可以工作了,我可以知道您现在收到的错误是什么吗?无限循环? 500错误? 404错误?你在用php吗?
    • 它只是说“无法访问此站点”。是的,PHP
    • 你做了 sudo service Nginx reload 吗?如果您已逐步遵循整个 DO 教程而没有遗漏任何内容。它将工作 100%。
    猜你喜欢
    • 2019-06-04
    • 2017-03-10
    • 2016-04-02
    • 1970-01-01
    • 2018-06-23
    • 2020-12-31
    • 2015-10-23
    相关资源
    最近更新 更多