深度阅读

How to redirect HTTP to HTTPS with nginx?

作者
作者
2023年08月22日
更新时间
10.01 分钟
阅读时间
0
阅读量

To redirect HTTP to HTTPS with nginx, you can add a new server block for HTTP that listens on port 80 and redirects to the corresponding HTTPS URL. Here are the general steps:

  1. Edit your nginx configuration file and locate the server block that serves HTTP requests on port 80.
  2. Add a new server block that listens on port 80 and redirects to the HTTPS URL. You can use the return directive to perform a 301 redirect, which is a permanent redirect that tells search engines to update their indexes with the new URL.
  3. In the new server block, specify the location of the HTTPS URL.

Here’s an example of what the new server block might look like:

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

Note that you should replace example.com with your own domain name, and customize the redirect URL as needed.

After you’ve made changes to the nginx configuration file, you need to reload the nginx service to apply the changes.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。