简体   繁体   English

将 HTTP 重定向到 HTTPS(中间件重定向 vs Nginx)

[英]Redirect HTTP to HTTPS (Middleware redirect vs Nginx)

I want to ask about HTTP-to-HTTPS redirections.我想问一下HTTP-to-HTTPS重定向。 As we know WWW-to-none-WWW redirections happen by redirecting from the web server side.正如我们所知, WWW-to-none-WWW重定向是通过从 Web 服务器端重定向来发生的。 But when it comes to the https redirection, it can be done by both ways, server-side ( Nginx etc... ) and application-side ( Middleware ).但是当涉及到 https 重定向时,它可以通过服务器端( Nginx etc... )和应用程序端( Middleware )两种方式来完成。 I want to know:我想知道:

  1. Which of the ways are effective and have more performance.哪种方式有效且性能更高。
  2. The pros and cons of each way considering multiple top-level domains and sub-domain domains on the same server.考虑同一服务器上的多个top-level域和sub-domain域的每种方式的优缺点。

Thank you.谢谢你。

Reference:参考:

  1. Redirect WWW to non-WWW in Laravel - Stack overflow在 Laravel 中将 WWW 重定向到非 WWW - 堆栈溢出
  2. Redirect HTTP to HTTPS in Laravel - Stack overflow在 Laravel 中将 HTTP 重定向到 HTTPS - 堆栈溢出
  3. HTTP Request To HTTPS on nginx - nixCraft nginx 上对 HTTPS 的 HTTP 请求 - nixCraft

Server-based redirection here should be more performant because it happens before any application code gets loaded.此处基于服务器的重定向应该具有更高的性能,因为它发生在加载任何应用程序代码之前。

Personally, I always do this in the nginx server{} block for all sites.就我个人而言,我总是在所有站点的 nginx server{} 块中执行此操作。 I create a conf file for a domain and have 2 server{} blocks, a main one listening on 443 for HTTPS traffic, and a small one that just recognises the (sub)domain and does a redirect to the HTTPS protocol.我为域创建了一个 conf 文件,并有 2 个服务器块,一个主要用于侦听 443 以获取 HTTPS 流量,一个小块仅识别(子)域并重定向到 HTTPS 协议。{}

Here's an example redirect server{} block I have for a particular subdomain:这是我针对特定子域的示例重定向服务器{}块:

server {
    server_tokens off;
    listen 80;
    server_name sub.domain.com;
    return 301 https://sub.domain.com$request_uri;
}

As for pros cons for server-based, the obvious ones I would say are:至于基于服务器的利弊,我想说的显而易见的是:

Pros优点

  • Performance表现
  • Simplicity简单

Cons缺点

  • root access required (for nginx at least, Apache you could do it in a .htaccess file, but this in itself has performance costs)需要 root 访问权限(至少对于 nginx,Apache 您可以在 .htaccess 文件中执行此操作,但这本身具有性能成本)
  • Can't change things on the fly so easily (flexibility?)不能如此轻松地动态改变事物(灵活性?)

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

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