如何在 https NGINX 中将非 www 重定向到 www [英] How can I redirect non-www to www in https NGINX

查看:73
本文介绍了如何在 https NGINX 中将非 www 重定向到 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 Nginx 重定向相关的问题波纹管你可以看到配置.我的目标是从 https://example.com 重定向到 https://www.example.com

I have a question related with Nginx redirects Bellow you can see configurations. My goal is to redirect from https://example.com to https://www.example.com

我几乎在stackoverflow中查看了所有内容,但没有找到任何帮助.请帮我解决这个问题.我将提供有关我的 Nginx Web 服务器的所有必要信息.我希望你能帮助我解决这个难题.

I looked through almost all in stackoverflow and I didn't find any help. Please help me with this issue. I will provide all necessary information about my Nginx Web Server. I hope you will help me, with this difficult question.

我的文件 nginx.conf 看起来像那里:

My file nginx.conf looks like there:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections 768;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  server_names_hash_bucket_size 64;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;
  gzip_static on;
  gzip_disable "msie6";

  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 9;
  # gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xm$

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

我的文件 /etc/nginx/sites-enabled/example:

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

server {
  listen 443 ssl;
  server_name www.example.com;
  ssl_stapling on;
  ssl on;
  ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot

  root /var/www/example/public;
  index ../views/index.html;

  location /img/ {
    proxy_pass http://127.0.0.1:3010;
    proxy_cache off;
    proxy_cache_key "$proxy_host$uri$is_args$args";
  }

  location / {
    proxy_pass http://127.0.0.1:3010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

  location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|css|js|html)$ {
    root /var/www/example/public;
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
  }
}

推荐答案

只需为非 www 请求创建一个服务器,例如:

Just create a server for non-www requests, for example:

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

# redirect http://example.com to https://www.example.com
server {
    listen 443 ssl;
    server_name example.com;

    # ssl ...

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

# https://www.example.com
server {
    listen 443 ssl;
    server_name www.example.com;

    # ssl ...
}

example.comwww.example.com 的 DNS 记录应该指向您的 Nginx 服务器

The DNS records for example.com and www.example.com should be pointing to your Nginx server

这篇关于如何在 https NGINX 中将非 www 重定向到 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆