2017-12-20 18 views
1

모든 HTTP 리디렉션 : 내가 직접 https://example.com을 입력하면, 지금의 nginx가 나는 <code>https://example.com.</code> 내가 원하는 모든 웹 사이트 트래픽을 리디렉션하려고 HTTPS

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` 

, 그것을 작동합니다.

그러나, 나는 입력하면 비 SSL example.com 또는 www.example.com 또는 http://example.com 또는 http://www.example.com,이 오류가 얻을 : 내가 내의 nginx를 수정해야 생각

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

을하는 아래에 있습니다.

# 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 

} 

} 
+1

마지막 세 줄의 주석을 지우지 않는 이유는 무엇입니까? :). 그것이 nginx에서 요청을 https로 리디렉션하는 방법입니다. – mohessaid

답변

1

코드에서 두 서버를 분리해야합니다. 포트 80 또는 http을 서버 절에 넣고 https을 다른 서버 절에 넣습니다. 리디렉션은 return 301 https://$host$request_uri; 행 때문에 발생합니다. https가 아닌 http 섹션에 있어야합니다. 구성을 보면 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 
} 
} 

}