问题描述:

I have my server in DO running in Ubuntu 16.04.

My problem is that my site wont load when I try to run without www.

I followed the tutorial from here.

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

My goal is to

  1. redirect all non-www to https://example.com
  2. redirect all www to 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;

}

}

What I have tried so far:

Removing http in first and second server block. (same in second server block)

server {

listen 80;

server_name example.com;

return 301 https://example.com$request_uri;

}

网友答案:

To achieve your goal
1. redirect all non-www to https://example.com 2. redirect all www to https://www.example.com This can be done by

server_name www.example.com example.com;

You can use full configuration like following

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;
}
}
网友答案:

dont put 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;
    }
 }
相关阅读:
Top