VPS侦探论坛

 找回密码
 注册
查看: 5902|回复: 7

老站301问题,求助

[复制链接]
发表于 2021-5-31 15:59:11 | 显示全部楼层 |阅读模式

之前是可以的,但最近换了空间,又重新装了lnmp 参考了官网上的301设置文章:LNMP下Nginx如何设置域名301重定向
重新设置,但一直不起作用。
我看里面写的,把301那段代码,是加在最后面的。我就把配置文件里面,也是加到最后面的,但重启之后,不起作用。
  1. server {
  2. listen 443 ssl;
  3. server_name www.lnmp.org;
  4. 省略其他配置
  5. }
  6. server {
  7. listen 80;
  8. server_name lnmp.org;
  9. return 301 https://www.lnmp.org$request_uri;
  10. }
复制代码
原文章里就是这样写的,第二server就是301,我也按这个设置,在域名.conf里面,加上了这段并替换好了域名,但重启后无效。
因为是老站,是我加错了么,配置文件如下:[我把自己的网址替换成了军哥的]
  1. server
  2.     {
  3.         listen 80;
  4.         listen 443 ssl;
  5.         ssl on;
  6.         ssl_certificate /etc/ssl/cert.pem;
  7.         ssl_certificate_key /etc/ssl/key.pem;
  8.         #listen [::]:80;
  9.         server_name vpser.net www.vpser.net;
  10.         index index.html index.htm index.php default.html default.htm default.php;
  11.         root  /home/wwwroot/www.vpser.net;

  12.         include rewrite/discuzx.conf;
  13.         error_page   404   /404.htm;
  14.         # Deny access to PHP files in specific directory
  15.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  16.         include enable-php.conf;

  17.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  18.         {
  19.             expires      30d;
  20.         }

  21.         location ~ .*\.(js|css)?$
  22.         {
  23.             expires      12h;
  24.         }

  25.         location ~ /.well-known {
  26.             allow all;
  27.         }

  28.         location ~ /\.
  29.         {
  30.             deny all;
  31.         }

  32.         access_log off;
  33.     }
  34. server {
  35. listen 80;
  36. server_name vpser.net www.vpser.net;
  37. if ($host = 'vpser.net') {
  38.   return 301 https://www.vpser.net$request_uri;
  39. }
  40. }
复制代码



美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
 楼主| 发表于 2021-6-4 17:29:34 | 显示全部楼层


测试了几天发现了一个问题,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 所写,仅供后来者参考。

  1. server
  2.     {
  3.         listen 443 ssl;
  4.         ssl on;
  5.         ssl_certificate /etc/ssl/cert.pem;
  6.         ssl_certificate_key /etc/ssl/key.pem;
  7.         #listen [::]:80;
  8.         server_name www.123.com;
  9.         index index.html index.htm index.php default.html default.htm default.php;
  10.         root  /home/wwwroot/www.123.com;
  11.         if ($host = '123.com') {return 301 https://www.123.com$request_uri;}

  12.         include rewrite/discuzx.conf;
  13.         error_page   404   /404.htm;
  14.         if ($scheme = http ) {return 301 https://$host$request_uri;}
  15.         # Deny access to PHP files in specific directory
  16.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  17.         include enable-php.conf;

  18.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  19.         {
  20.             expires      30d;
  21.         }

  22.         location ~ .*\.(js|css)?$
  23.         {
  24.             expires      12h;
  25.         }

  26.         location ~ /.well-known {
  27.             allow all;
  28.         }

  29.         location ~ /\.
  30.         {
  31.             deny all;
  32.         }

  33.         access_log off;
  34.     }
  35. server {
  36. listen 80;
  37. server_name 123.com www.123.com;
复制代码


Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
 楼主| 发表于 2021-5-31 16:11:46 | 显示全部楼层

参考了别人的有效设置,重新更改了一下,发现还是不起作用
  1. server
  2.     {
  3.         listen 80;
  4.         listen 443 ssl;
  5.         ssl on;
  6.         ssl_certificate /etc/ssl/cert.pem;
  7.         ssl_certificate_key /etc/ssl/key.pem;
  8.         #listen [::]:80;
  9.         server_name www.123.com;
  10.         index index.html index.htm index.php default.html default.htm default.php;
  11.         root  /home/wwwroot/www.123.com;

  12.         include rewrite/discuzx.conf;
  13.         error_page   404   /404.htm;
  14.         if ($scheme = http ) {return 301 https://$host$request_uri;}
  15.         # Deny access to PHP files in specific directory
  16.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  17.         include enable-php.conf;

  18.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  19.         {
  20.             expires      30d;
  21.         }

  22.         location ~ .*\.(js|css)?$
  23.         {
  24.             expires      12h;
  25.         }

  26.         location ~ /.well-known {
  27.             allow all;
  28.         }

  29.         location ~ /\.
  30.         {
  31.             deny all;
  32.         }

  33.         access_log off;
  34.     }
  35. server {
  36. listen 80;
  37. server_name 123.com;
  38. return 301 https://www.123.com$request_uri;
  39. }
复制代码


美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
 楼主| 发表于 2021-5-31 16:12:38 | 显示全部楼层



我用的是lnmp1.7
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
 楼主| 发表于 2021-5-31 18:05:54 | 显示全部楼层

在线等军哥指导。

军哥运维代购:http://shop63846532.taobao.com/

发表于 2021-5-31 19:12:13 | 显示全部楼层

教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件
 楼主| 发表于 2021-5-31 21:25:33 | 显示全部楼层

licess 发表于 2021-5-31 19:12
教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件 ...


军哥,每次更改我都重启了Lnmp 现在我又改了一下,请你看看是否改对了。
我用这个改,用站长工具查了之后,结果是对的,但还是不跳转。
  1. server
  2.     {
  3.         listen 443 ssl;
  4.         ssl on;
  5.         ssl_certificate /etc/ssl/cert.pem;
  6.         ssl_certificate_key /etc/ssl/key.pem;
  7.         #listen [::]:80;
  8.         server_name www.123.com;
  9.         index index.html index.htm index.php default.html default.htm default.php;
  10.         root  /home/wwwroot/www.123.com;

  11.         include rewrite/discuzx.conf;
  12.         error_page   404   /404.htm;
  13.         if ($scheme = http ) {return 301 https://$host$request_uri;}
  14.         # Deny access to PHP files in specific directory
  15.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  16.         include enable-php.conf;

  17.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  18.         {
  19.             expires      30d;
  20.         }

  21.         location ~ .*\.(js|css)?$
  22.         {
  23.             expires      12h;
  24.         }

  25.         location ~ /.well-known {
  26.             allow all;
  27.         }

  28.         location ~ /\.
  29.         {
  30.             deny all;
  31.         }

  32.         access_log off;
  33.     }
  34. server {
  35. listen 80;
  36. server_name 123.com;
  37. return 301 https://www.123.com$request_uri;
  38. }
复制代码


美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
 楼主| 发表于 2021-5-31 21:31:46 | 显示全部楼层

licess 发表于 2021-5-31 19:12
教程里可没让你80和443在一个server段了
配置文件改完重启了没,按反馈必读里面说明测试配置文件 ...

好了,仔细的读了你那篇文章,现在成功的跳转了。再次感谢。照例附上最终修改版:
  1. server
  2.     {
  3.         listen 443 ssl;
  4.         ssl on;
  5.         ssl_certificate /etc/ssl/cert.pem;
  6.         ssl_certificate_key /etc/ssl/key.pem;
  7.         #listen [::]:80;
  8.         server_name www.123.com;
  9.         index index.html index.htm index.php default.html default.htm default.php;
  10.         root  /home/wwwroot/www.123.com;
  11.         if ($host = '123.com') {return 301 https://www.123.com$request_uri;}

  12.         include rewrite/discuzx.conf;
  13.         error_page   404   /404.htm;
  14.         if ($scheme = http ) {return 301 https://$host$request_uri;}
  15.         # Deny access to PHP files in specific directory
  16.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  17.         include enable-php.conf;

  18.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  19.         {
  20.             expires      30d;
  21.         }

  22.         location ~ .*\.(js|css)?$
  23.         {
  24.             expires      12h;
  25.         }

  26.         location ~ /.well-known {
  27.             allow all;
  28.         }

  29.         location ~ /\.
  30.         {
  31.             deny all;
  32.         }

  33.         access_log off;
  34.     }
  35. server {
  36. listen 80;
  37. server_name 123.com;
  38. return 301 https://www.123.com$request_uri;
  39. }
复制代码




补充内容 (2021-6-4 17:26):
server {
listen 80;
server_name www.123.com 123.com;
return 301 https://www.123.com$request_uri;
}
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|VPS侦探 ( 鲁ICP备16040043号-1 )

GMT+8, 2024-9-21 00:52 , Processed in 0.027717 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表