您好,如果有人可以帮助完成以下 Nginx 配置,我将不胜感激:
/etc/nginx/sites-available/example
server {
server_name *.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/admin/pyapps/example/example/static/;
}
location /static/admin/ {
alias /home/admin/pyapps/example/example/static/admin/;
}
location /media/ {
root /home/admin/pyapps/example;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
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
}
server {
if ($host = admin.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = backoffice.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.com *.example.com;
return 404; # managed by Certbot
}
我想为 example.com 创建一个重定向到 https://www.example.com。
请注意,我正在使用 django-hosts。
我的 django-hosts 配置如下:
设置.py
ROOT_URLCONF = 'example.urls'
ROOT_HOSTCONF = 'example.hosts'
DEFAULT_HOST = 'www'
# DEFAULT_REDIRECT_URL = "http://www.example.com:8000"
DEFAULT_REDIRECT_URL = "http://www.example.com"
PARENT_HOST = 'example.com'
# HOST_PORT = '8000'
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
....
'django_hosts.middleware.HostsResponseMiddleware',
]
主机.py
from django.conf import settings
from django_hosts import patterns, host
from example.hostsconf import urls as redirect_urls
from . import admin_urls
host_patterns = patterns('',
host(r'www', settings.ROOT_URLCONF, name='www'),
host(r'admin', admin_urls, name='admin'),
host(r'backoffice', 'backoffice.urls', name="backoffice"),
host(r'(?!www).*', redirect_urls, name='wildcard'),
)
hostsconf/urls.py
from django.urls import path
from .views import wildcard_redirect
urlpatterns = [
path(r'^?P<path>.*)', wildcard_redirect),
]
hostsconf/views.py
from django.conf import settings
from django.http import HttpResponseRedirect
DEFAULT_REDIRECT_URL = getattr(settings, "DEFAULT_REDIRECT_URL", "http://www.example.com")
def wildcard_redirect(request, path=None):
new_url = DEFAULT_REDIRECT_URL
if path is not None:
new_url = DEFAULT_REDIRECT_URL + '/' + path
return HttpResponseRedirect(new_url)
我几乎尝试了所有方法,但我认为这可能与 django-hosts 配置有关。
当我访问 https://example.com 时,它会向我显示“欢迎使用 nginx”屏幕,而不是重定向到 https://www.example.com。
当我访问 www.example.com 或 admin.example.com 或 backoffice.example.com 时,网站加载正常。
请有人帮助我。我想将 https://example.com 重定向到 https://www.example.com
如果您需要我提供任何其他配置,请告诉我。
从您的第一个服务器块中删除
example.com
,因为如果没有 HTTPS 子域,您将不会向该域提供任何服务。
将以下 if 语句添加到第二个服务器块,将非 www 流量直接重定向到 www HTTPS 版本:
if ($host = example.com) { return 301 https://www.example.com$request_uri; }
添加包含以下内容的第三个服务器块,以将 HTTPS 流量从非 www 重定向到 www:
server {
if ($host = example.com) {
return 301 https://www.example.com$request_uri;
}
server_name example.com;
listen [::]:443; # if you would like to support IPv6
listen 443;
return 404;
}