简体   繁体   English

Nginx 重定向 HTTP 到 HTTPS 和非 www 到 ww

[英]Nginx Redirect HTTP to HTTPS and non-www to ww

I'm setting up an Nginx server with an SSL.我正在使用 SSL 设置 Nginx 服务器。

The domain with the SSL is www.mydomain.example SSL 的域是www.mydomain.example

I want to redirect all requests from:我想重定向所有请求:

http://mydomain.example , http://www.mydomain.example , & https://mydomain.example to http://mydomain.example , http://www.mydomain.example , & https://mydomain.example

https://www.mydomain.example

I have the following server blocks setup currently:我目前有以下服务器块设置:

server{
  listen 443 ssl;
  root /www/mydomain.example/;

  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  .
  .
}

server{
  listen 80;
  server_name mydomain.example;
  return 301 https://www.mydomain.example$request_uri;
}

server{
  listen 80;
  server_name www.mydomain.example;
  return 301 https://www.mydomain.example$request_uri;
}

server{
  listen ssl 443;
  server_name mydomain.example;
  return 301 https://www.mydomain.example$request_uri;
}

This currently does not work, but I don't understand why not.这目前不起作用,但我不明白为什么不这样做。 I can get a combination of either HTTP -> HTTPS working or no-www to -> www working, but mixing them as above does not work.我可以得到 HTTP -> HTTPS 工作或 no-www 到 -> www 工作的组合,但如上所述混合它们不起作用。

The SSL redirect won't work if your SSL certificate doesn't support the non-www domain.如果您的 SSL 证书不支持非 www 域,则 SSL 重定向将不起作用。 The config is correct but can be reduced to just 1 redirect server配置是正确的,但可以减少到只有 1 个重定向服务器

Also don't forget to reload Nginx sudo service nginx reload也不要忘记重新加载 Nginx sudo service nginx reload

server {
  listen 80;
  listen 443 ssl;
  server_name example.com;
  # add ssl settings
  return 301 https://www.example.com$request_uri;
}

I am late, But you can do like this我迟到了,但你可以这样做

server{
  listen 443 ssl;
  server_name www.mydomain.example;
  root /www/mydomain.example/;
  
  ssl    on;
  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

server{
  listen 80;
  server_name www.mydomain.example mydomain.example;
  return 301 https://$server_name$request_uri;
}

server{
  listen 443;
  server_name mydomain.example;
  return 301 https://www.$server_name$request_uri;
}

Or you can replace return 301 https://www.$server_name$request_uri;或者你可以替换return 301 https://www.$server_name$request_uri; with rewrite ^ http://www.$server_name$request_uri? permanent; rewrite ^ http://www.$server_name$request_uri? permanent; rewrite ^ http://www.$server_name$request_uri? permanent; , both will work. ,两者都可以。

You also need to set this in google webmaster for better SEO.您还需要在 google 网站管理员中设置此项以获得更好的 SEO。

this works for me for HTTP to HTTPS redirection,这适用于我的 HTTP 到 HTTPS 重定向,

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name example.com;

    #For HTTP to HTTPS:

    proxy_set_header X-Forwarded-Proto $scheme;
    if ( $http_x_forwarded_proto != 'https' )
    {
        return 301 https://$host$request_uri;
    }

    location / {
        try_files $uri $uri/ /index.php;
        add_header 'Access-Control-Allow-Origin' '*';
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

I was trying to fix the problem by redirecting the request URI to HTTPS if it was requested with the HTTP scheme.如果使用HTTP方案请求它,我试图通过将请求 URI 重定向到HTTPS来解决问题。 But this solution does not work in some conditions.但是这种解决方案在某些情况下不起作用。

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

When we type a domain name in the search bar of a browser, it makes a request with the HTTP scheme by default.当我们在浏览器的搜索栏中输入域名时,它会默认使用HTTP方案进行请求。 This behavior of the browser is not handled by , but it can be done manually.浏览器的这种行为不是由处理的,但可以手动完成。 We have to handle 497 status code by adding error_page 497 https://$server_name$request_uri;我们必须通过添加error_page 497 https://$server_name$request_uri;来处理497状态码; . .

error_page 497 https://$server_name$request_uri;

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

What does the 497 indicate, and when occur? 497 表示什么,何时发生?

The 497 HTTP Request Sent to HTTPS Port is used when a client has made an HTTP request to a port that is listening for HTTPS requests specifically. The 497 HTTP Request Sent to HTTPS Port is used when a client has made an HTTP request to a port that is listening for HTTPS requests specifically.

#If you like to redirect all "http" to "https" then add the following:
server {
        listen 80;

        server_name yourdomain.example;
        server_name www.yourdomain.example;

 if ($scheme = "http")
        {
                rewrite ^(.*)$ https://yourdomain.example$1 permanent;
        }
}

Use a rewrite to send all HTTP traffic to HTTPS:使用重写将所有 HTTP 流量发送到 HTTPS:

server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name _;
 return 301 https://$host$request_uri;
}

This configuration listens on port 80 as the default server for both IPv4 and IPv6 and for any hostname.此配置侦听端口 80 作为 IPv4 和 IPv6 以及任何主机名的默认服务器。 The return statement returns a 301 permanent redirect to the HTTPS server at the same host and request URI. return 语句将 301 永久重定向返回到同一主机上的 HTTPS 服务器和请求 URI。

Please add two given things on your file.请在您的文件中添加两个给定的内容。

Code to paste on top:要粘贴在顶部的代码:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

Code to paste on bottom:粘贴在底部的代码:

server {
    listen 443 ssl http2;
    server_name www.example.com;

    # . . . other code

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

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM