Ubuntu Nginx强制重定向https://www.domain 到https://domain
基本上,我尝试从以下位置重定向用户:Ubuntu Nginx强制重定向https://www.domain 到https://domain,ubuntu,redirect,nginx,gunicorn,Ubuntu,Redirect,Nginx,Gunicorn,基本上,我尝试从以下位置重定向用户: domain.com www.domain.com 永远。我当前的Nginx配置如下: upstream app_server { # Bindings to the Gunicorn server server 127.0.0.1:8002 fail_timeout=0; } server { server_name "~^www\.(.*)$" ; return 301 https://$1$request_uri
- domain.com
- www.domain.com
upstream app_server {
# Bindings to the Gunicorn server
server 127.0.0.1:8002 fail_timeout=0;
}
server {
server_name "~^www\.(.*)$" ;
return 301 https://$1$request_uri ;
}
server {
# Access Logs
access_log path_to/nginx-access.log;
error_log path_to/nginx-error.log;
listen 443 ssl;
server_name _;
client_max_body_size 4G;
keepalive_timeout 5;
root another_path;
ssl_certificate path_to.crt;
ssl_certificate_key path_to.key;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root %(PROJECT_PATH)s/templates/public;
}
}
server {
listen 443 ssl http2 deffered;
server_name www.domain.com;
return 301 https://domain.com$request_uri;
}
问题是,我的证书仅对domain.com有效(不使用www前缀),因此用户在访问最后一个地址()时会出错
我错过了什么?如何重定向到?我认为这里的问题是您没有捕获第一个服务器块。如果排除
listen
指令,则默认情况下,该服务器块将在80上侦听
为此特殊情况创建另一个服务器块,如下所示:
upstream app_server {
# Bindings to the Gunicorn server
server 127.0.0.1:8002 fail_timeout=0;
}
server {
server_name "~^www\.(.*)$" ;
return 301 https://$1$request_uri ;
}
server {
# Access Logs
access_log path_to/nginx-access.log;
error_log path_to/nginx-error.log;
listen 443 ssl;
server_name _;
client_max_body_size 4G;
keepalive_timeout 5;
root another_path;
ssl_certificate path_to.crt;
ssl_certificate_key path_to.key;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root %(PROJECT_PATH)s/templates/public;
}
}
server {
listen 443 ssl http2 deffered;
server_name www.domain.com;
return 301 https://domain.com$request_uri;
}