从Nginx重定向HTTP到HTTPS不起作用

mkh04yzy  于 2024-01-06  发布在  Nginx
关注(0)|答案(7)|浏览(256)

类型:https://example.com =>ssl ok但类型:www.example.com和example.com是httpno redirecthttps.(www重定向到非www)。
WordPress地址(URL)和站点地址(URL):https//example.com
/etc/nginx/conf.d/example.com.conf

  1. server {
  2. listen 80;
  3. server_name example.com www.example.com;
  4. return 301 https://$server_name$request_uri;
  5. }
  6. server {
  7. listen 443 ssl;
  8. server_name example.com www.example.com;
  9. ssl on;
  10. ssl_certificate /etc/nginx/ssl/cert_chain.crt;
  11. #ssl_CACertificate_File /etc/nginx/ssl/example.com.ca-bundle;
  12. ssl_certificate_key /etc/nginx/ssl/example.com.key;
  13. access_log off;
  14. # access_log /home/example.com/logs/access_log;
  15. error_log off;
  16. # error_log /home/example.com/logs/error.log;
  17. add_header X-Frame-Options SAMEORIGIN;
  18. add_header X-Content-Type-Options nosniff;
  19. add_header X-XSS-Protection "1; mode=block";
  20. root /home/example.com/public_html;
  21. include /etc/nginx/conf/ddos2.conf;
  22. index index.php index.html index.htm;
  23. server_name example.com;

字符串
怎么修?抱歉我英语不好,谢谢。

x6yk4ghg

x6yk4ghg1#

这可能是由于服务器名称不明确造成的。请尝试使用以下命令:

  1. server {
  2. server_name example.com www.example.com;
  3. listen 80;
  4. listen 443 ssl; # Listen for SSL at port 443 as well
  5. # ... other config - certificates and such
  6. # If a user tries to come through http, redirect them through https
  7. if ($scheme != "https") {
  8. return 301 https://$host$request_uri;
  9. }
  10. }

字符串
你可以通过运行sudo nginx -t来检查你的nginx配置。

展开查看全部
gmol1639

gmol16392#

我也遇到过类似的问题。这是由Linux防火墙引起的(端口80被禁止)。

kg7wmglp

kg7wmglp3#

您的配置不清楚,您在末尾有一个重复的server_name example.com行。
尝试使用这个:

  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server;
  4. server_name example.com www.example.com;
  5. return 301 https://$server_name$request_uri;
  6. }

字符串

h7appiyu

h7appiyu4#

更新此行返回301 https://$server_name$request_uri;
返回301 https://$http_host$request_uri

brc7rcf0

brc7rcf05#

我也有同样的问题与nginx,所以我应用这些服务器只接受一个请求,这将决定为http和https

  1. server {
  2. listen 80 ;
  3. listen [::]:80 ;
  4. listen 443 ssl http2 ;
  5. listen [::]:443 ssl http2 ;
  6. server_name example.com www.example.com;
  7. #------ ssl certificates and other config --------
  8. set $https_redirect 0;
  9. #if request came from port 80/http
  10. if ($server_port = 80) {
  11. set $https_redirect 1;
  12. }
  13. # or if the requested host came with www
  14. if ($host ~ '^www\.') {
  15. set $https_redirect 1;
  16. }
  17. #then it will redirects
  18. if ($https_redirect = 1) {
  19. return 301 https://example.com$request_uri;
  20. }
  21. }

字符串
通过使用这个,我只有服务器块来处理任何请求

展开查看全部
ygya80vv

ygya80vv6#

如果您的域设置位于

  1. /etc/nginx/sites-available/default

字符串
或者你可以在/etc/nginx/sites-available/domain.com上为每个域名设置不同的文件。

  1. server {
  2. if ($host = www.domainname1.com) {
  3. return 301 https://$host$request_uri;
  4. }
  5. if ($host = www.domainname2.com) {
  6. return 301 https://$host$request_uri;
  7. }
  8. listen 80;
  9. listen [::]:80;
  10. server_name www.domainname1.com www.domainname2.com;
  11. return 404;
  12. }

展开查看全部
vkc1a9a2

vkc1a9a27#

这段代码将帮助你重定向到https:
假设您输入http://www.example.com,则它将重定向到https://www.example.com
,如果有人输入http:example.com,它将重定向到https://www.example.com

  1. <VirtualHost *:80>
  2. ServerName www.example.com
  3. DocumentRoot /usr/local/apache2/htdocs
  4. Redirect permanent / https://www.example.com/
  5. </VirtualHost>
  6. <VirtualHost_default_:443>
  7. ServerName www.example.com
  8. DocumentRoot /usr/local/apache2/htdocs
  9. SSLEngine On
  10. RewriteCond %{HTTPS} off
  11. RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
  12. RewriteRule (.*) https://www.example.com/$1 [R=301,L]
  13. RewriteCond %{HTTPS} off
  14. RewriteCond %{HTTP_HOST} ^example\.com$
  15. RewriteRule (.*) https://www.example.com/$1 [R=301,L]
  16. </VirtualHost>

字符串

展开查看全部

相关问题