4

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

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

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

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";
  }
}
4

2 回答 2

8

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

# 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.com并且www.example.com应该指向您的 Nginx 服务器的 DNS 记录

于 2018-07-23T13:46:29.043 回答
2

重定向和 ssl 的快速说明

不要将所有站点的所有配置都写在一个文件中nginx.conf。把这些分开。你有两个文件夹/etc/nginx/sites-available//etc/nginx/sites-enabled/

为您的网站添加文件,例如/etc/nginx/sites-available/example 制作链接ln -s /etc/nginx/sites-enabled/example

在这个 conf 文件中粘贴下面的文本:

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

server {
  listen 443 ssl;
  server_name www.example.com;
  ssl_stapling on;
  ssl on;
  ssl_certificate /etc/letsencrypt/live/www.site.com/fullchain.pem;     
  ssl_certificate_key /etc/letsencrypt/live/www.site.com/privkey.pem;
  # your location there a
}

在您nginx.conf的 ypu 中include /etc/nginx/sites-enabled/*;,这意味着自动从文件夹中获取所有站点配置sites-enabled

在使用命令检查语法并使用命令nginx -t重新加载您的 nginx之后systemctl reload nginx

毕竟,通过http://example.comhttps://example.com调用您的网站的人将被重定向到https://www.example.com

于 2018-07-23T08:47:42.590 回答