【问题标题】:Trying to set Http to Https Redirect on Nginx尝试在 Nginx 上将 Http 设置为 Https Redirect
【发布时间】:2020-06-14 18:28:04
【问题描述】:

我已经设置了我的服务器并在 Vultr VPS 上部署了项目,我正在尝试为我的域设置重定向,但我不断收到此错误 This page isn’t working henryestate.xyz redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

我试图编辑我的etc/nginx/sites-enabled/default

我有以下代码:

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

  # SSL configuration
  #
  listen 443 ssl;
  # listen [::]:443 ssl default_server;
  #
  # Note: You should disable gzip for SSL traffic.
  # See: https://bugs.debian.org/773332
  #
  # Read up on ssl_ciphers to ensure a secure configuration.
  # See: https://bugs.debian.org/765782
  #
  # Self signed certs generated by the ssl-cert package
  # Don't use them in a production server!
  #
  # include snippets/snakeoil.conf;
  root /var/www/laravel/public;

  # Add index.php to the list if you are using PHP
  index index.php index.html index.htm;

  server_name henryestate.xyz www.henryestate.xyz;

  return 301 https://henryestate.xyz$request_uri;


  ssl_certificate /etc/nginx/ssl/cert_chain.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php?$query_string;
  }

  # pass PHP scripts to FastCGI server
  #
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    #
    # # With php-fpm (or other unix sockets):
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    # # With php-cgi (or other tcp sockets):
    # fastcgi_pass 127.0.0.1:9000;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  location ~ /\.ht {
    deny all;
  }
}

我如何对这个 https 重定向进行排序,以允许网站在 www 和非 www 时都使用 https 加载

【问题讨论】:

    标签: ssl nginx


    【解决方案1】:

    如果您的意思是从 HTTP 重定向到 HTTPS 并保留服务器名称(www 或非 www),请使用两个不同的 server 块:

    server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name henryestate.xyz www.henryestate.xyz;
      return 301 https://$host$request_uri;
    }
    
    server {
      listen 443 ssl;
      server_name henryestate.xyz www.henryestate.xyz;
      ...
    }
    

    从第二个server 块中删除return 301 https://henryestate.xyz$request_uri;

    最好使用try_files $uri $uri/ /index.php$is_args$args;,而不是try_files $uri $uri/ /index.php?$query_string;

    【讨论】:

    • try_files $uri $uri/ /index.php$is_args$args; 会是哪个块?
    • @TobiFerdinand 你的配置中只有一个 try_files 指令,location / { ... } 块。
    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2021-10-15
    • 2014-10-02
    相关资源
    最近更新 更多