http - nginx 将所有 http 重定向到 https

标签 http nginx https

我正在尝试将所有网站流量重定向到 https://example.com。 我要:

https://example.com
https://www.example.com
http://example.com
http://www.example.com
example.com
www.example.com

to all redirect to `https://example.com`

现在,如果我直接输入 https://example.com,就可以了。

但是,如果我输入非 SSL example.com 或 www.example.com 或 http://example.comhttp://www.example.com ,我得到这个错误:

This site can’t be reached
vinnect.com refused to connect.
Search Google for vinnect
ERR_CONNECTION_REFUSED

我认为我需要修复我的 nginx,如下所示。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

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

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

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

index   index.html index.htm;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # 
managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # 
managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



# Redirect non-https traffic to https
# if ($scheme != "https") {
#     return 301 https://$host$request_uri;
# } # managed by Certbot

}

}

最佳答案

在您的代码中,您必须将两个服务器分开。将端口 80 或 http 放在服务器子句中,将 https 放在另一个服务器子句中。 由于 return 301 https://$host$request_uri; 行,将发生重定向。它应该在 http 部分而不是 https。 查看您的配置,我注意到最后三行正在检查非 https 请求并使用相同的方法将用户重定向回 https。我从未尝试过,但它可能会奏效,因为 Cerbot 的人知道他们在做什么。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

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

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

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

index   index.html index.htm;

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

server {
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # 
    managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # 
    managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot
}
}

}

关于http - nginx 将所有 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898441/

相关文章:

AngularJS $http.post() 到同一台机器上的另一个端口

javascript - AJAX 请求从 HTTPS 页面到 HTTP Url

ruby-on-rails - Nginx + Phusion 乘客 try_files

c# - HttpClient https 请求使用主机名失败,但使用 IP 地址有效

WCF、GET 和 HTTPS

javascript - NodeJS HTTPS 错误 (520)

reactjs - 跨源请求被阻止 React Golang

python - 使用 Python 和 CouchDB 在 App Engine 上进行奇怪的 HTTP 数据积累

apache - nginx 中的 $0 是什么? (mod_重写)

django - 更改文件后重新启动Gunicorn/Nginx