Nginx非www不工作,在Ubuntu 16.04中重定向
我的服务器在Ubuntu 16.04中运行。 我的问题是,当我尝试在没有www的情况下运行时,我的站点不会加载 我从这里开始学习教程。 我的目标是Nginx非www不工作,在Ubuntu 16.04中重定向,ubuntu,nginx,Ubuntu,Nginx,我的服务器在Ubuntu 16.04中运行。 我的问题是,当我尝试在没有www的情况下运行时,我的站点不会加载 我从这里开始学习教程。 我的目标是 将所有非www重定向到 将所有www重定向到 nginx/站点可用/nginx\u配置 到目前为止,我所尝试的: 正在删除第一个和第二个服务器块中的http。(在第二个服务器块中相同) 不要把http server { listen 80; server_name example.com; return 301 https://example.c
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;
}
}
不要把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;
}
}
实现您的目标1.将所有非www重定向到 2.将所有www重定向到 这可以通过以下方式完成:
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;
}
}
实现您的目标1.将所有非www重定向到 2.将所有www重定向到 这可以通过以下方式完成:
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;
}
}
谢谢你的回答。我试试这个。谢谢你的回答。我要试试这个,请看最新的问题。我包括了我到目前为止所做的尝试。谢谢你的回复,请问是哪个服务器块?我看不到任何服务器名称下的http。对不起,我现在看到了您更新的代码。您的服务器块现在应该可以工作了,我可以知道您现在收到的错误是什么吗?无限循环?500错误?404错误?你在使用php吗?它只是说“这个网站无法访问”。是的,PHPDid您是否执行sudo服务Nginx重新加载?如果您已经一步一步地完成了整个DO教程,并且没有遗漏任何内容。它将100%工作。请查看更新的问题。我包括了我到目前为止所做的尝试。谢谢你的回复,请问是哪个服务器块?我看不到任何服务器名称下的http。对不起,我现在看到了您更新的代码。您的服务器块现在应该可以工作了,我可以知道您现在收到的错误是什么吗?无限循环?500错误?404错误?你在使用php吗?它只是说“这个网站无法访问”。是的,PHPDid您是否执行sudo服务Nginx重新加载?如果您已经一步一步地完成了整个DO教程,并且没有遗漏任何内容。它将100%起作用。