1

我在 Laravel Forge 上使用 Let's Encrypt 设置了 HTTPS,并使用 Laravel 中间件将非安全域重定向到安全域:

if ( env('APP_ENV') === 'production' ) {
    $request->setTrustedProxies([$request->getClientIp()]);

    if ( !$request->secure() ) {
        return redirect()->secure($request->getRequestUri());
    }
}

return $next($request);

这是我的example.com(不是实际的域)Nginx 配置:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;
    root /home/forge/example.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/example.com/120143/server.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/120143/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers '[...]';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/example.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/after/*;

这是我的www.example.com配置:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.example.com/before/*;

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

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.example.com/after/*;

一切正常,HTTP重定向到HTTPS,www重定向到非www,但我想做相反的事情并将非www重定向到www。我添加了

return 301 $scheme://www.example.com$request_uri;

toexample.com并在配置中将其注释掉www.example.com以删除循环,但它不起作用:

www.example.com redirected you too many times.

我也尝试将非www配置的内容复制到www配置,并在那里听443,但它仍然无休止地重定向。我究竟做错了什么?感谢您的时间。

4

3 回答 3

1

一切正常,HTTP重定向到HTTPS,www重定向到非www,但我想做相反的事情并将非www重定向到www。

根据AWS EC2 负载均衡器后面的 Force WWWforge-conf/example.com/before/redirect.conf ,发生这种情况是因为您从未提及但仍必须包含的 default仍必须包含带有and的server定义,因此,当您尝试在自己的配置中执行相反的操作时,连同仍然必须保留的默认配置,您创建了一个完整的重定向循环。server_name www.example.comreturn 301 $scheme://example.com$request_uri;redirect

于 2017-08-04T19:39:51.447 回答
0

只需切换配置,通过在这些配置文件中将 www 添加到非 www 来切换。此外,因为您使用的是 Lets Encrypt for SSL,您可能需要切换配置ssl_certificatessl_certificate_key指向正确的文件,

ssl_certificate /etc/nginx/ssl/www.example.com/120143/server.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com/120143/server.key;

只需重新启动计算机即可刷新网络缓存。

于 2016-09-01T15:27:38.780 回答
0

我建议采用以下方法:

www.example.com应该看起来像

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.example.com/before/*;

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;
    root /home/forge/example.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/example.com/120143/server.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/120143/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers '[...]';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/example.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/www.example.com/after/*;

example.com应该看起来像

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/example.com/before/*;

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

include forge-conf/example.com/after/*;
于 2016-09-01T14:57:38.450 回答