老站301问题,求助
之前是可以的,但最近换了空间,又重新装了lnmp 参考了官网上的301设置文章:LNMP下Nginx如何设置域名301重定向重新设置,但一直不起作用。
我看里面写的,把301那段代码,是加在最后面的。我就把配置文件里面,也是加到最后面的,但重启之后,不起作用。server {
listen 443 ssl;
server_name www.lnmp.org;
省略其他配置
}
server {
listen 80;
server_name lnmp.org;
return 301 https://www.lnmp.org$request_uri;
}原文章里就是这样写的,第二server就是301,我也按这个设置,在域名.conf里面,加上了这段并替换好了域名,但重启后无效。
因为是老站,是我加错了么,配置文件如下:[我把自己的网址替换成了军哥的]
server
{
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
#listen [::]:80;
server_name vpser.net www.vpser.net;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/www.vpser.net;
include rewrite/discuzx.conf;
error_page 404 /404.htm;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server {
listen 80;
server_name vpser.net www.vpser.net;
if ($host = 'vpser.net') {
return 301 https://www.vpser.net$request_uri;
}
}
测试了几天发现了一个问题,http://www.123.com 不会跳转到https://www.123.com
但http://123.com 会跳转到https://www.123.com。
经折腾发现,最后一段没有写上 www.123.com 所以最终代码如下:
注:以下为自己参考https://lnmp.org/faq/lnmp-nginx-301-rewrite.html 所写,仅供后来者参考。
server
{
listen 443 ssl;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
#listen [::]:80;
server_name www.123.com;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/www.123.com;
if ($host = '123.com') {return 301 https://www.123.com$request_uri;}
include rewrite/discuzx.conf;
error_page 404 /404.htm;
if ($scheme = http ) {return 301 https://$host$request_uri;}
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server {
listen 80;
server_name 123.com www.123.com;
参考了别人的有效设置,重新更改了一下,发现还是不起作用
server
{
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
#listen [::]:80;
server_name www.123.com;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/www.123.com;
include rewrite/discuzx.conf;
error_page 404 /404.htm;
if ($scheme = http ) {return 301 https://$host$request_uri;}
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server {
listen 80;
server_name 123.com;
return 301 https://www.123.com$request_uri;
}
我用的是lnmp1.7 在线等军哥指导。 教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件 licess 发表于 2021-5-31 19:12
教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件 ...
军哥,每次更改我都重启了Lnmp 现在我又改了一下,请你看看是否改对了。
我用这个改,用站长工具查了之后,结果是对的,但还是不跳转。
server
{
listen 443 ssl;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
#listen [::]:80;
server_name www.123.com;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/www.123.com;
include rewrite/discuzx.conf;
error_page 404 /404.htm;
if ($scheme = http ) {return 301 https://$host$request_uri;}
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server {
listen 80;
server_name 123.com;
return 301 https://www.123.com$request_uri;
}
licess 发表于 2021-5-31 19:12
教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件 ...
好了,仔细的读了你那篇文章,现在成功的跳转了。再次感谢。照例附上最终修改版:
server
{
listen 443 ssl;
ssl on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
#listen [::]:80;
server_name www.123.com;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/www.123.com;
if ($host = '123.com') {return 301 https://www.123.com$request_uri;}
include rewrite/discuzx.conf;
error_page 404 /404.htm;
if ($scheme = http ) {return 301 https://$host$request_uri;}
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server {
listen 80;
server_name 123.com;
return 301 https://www.123.com$request_uri;
}
补充内容 (2021-6-4 17:26):
server {
listen 80;
server_name www.123.com 123.com;
return 301 https://www.123.com$request_uri;
}
页:
[1]