Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4b5d63

Browse files
Daniel Carreradannycarrera
Daniel Carrera
authored andcommitted
virtual_host_alias & ssl redirects with single 301
1 parent1e34489 commitb4b5d63

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ If you are using [letsencrypt-nginx-proxy-companion](https://github.com/JrCs/doc
101101

102102
This will setup the following redirects:
103103
-`http://example.com`→`https://example.com`
104-
-`http://www.example.com`→`https://www.example.com`→`https://example.com`
105-
-`http://old.example.com`→`http://example.com`→`https://example.com`
104+
-`http://www.example.com`→`https://example.com`
105+
-`http://old.example.com`→`https://example.com`
106106
-`https://www.example.com`→`https://example.com`
107107
-`https://old.example.com`→`https://example.com`
108108

‎nginx.tmpl‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,21 @@ server {
404404
# VIRTUAL_HOST_ALIAS
405405
{{range$host_alias,$containers:= groupByMulti$"Env.VIRTUAL_HOST_ALIAS""," }}
406406

407+
{{$host_alias:= trim$host_alias }}
408+
407409
{{$first_host:= (first (groupByKeys$containers"Env.VIRTUAL_HOST")) }}
410+
{{$first_host:= trim$first_host }}
411+
408412
# First Host {{$first_host }}
409413

410414
#Alias: {{$host_alias }}
411-
server {
412-
server_name {{$host_alias }};
413-
return 301$scheme://{{$first_host }}$request_uri;
414-
}
415415

416416
{{$default_host:=or ($.Env.DEFAULT_HOST)"" }}
417417
{{$default_server:=index (dict$host_alias""$default_host"default_server")$host_alias }}
418418

419+
{{/* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external"*/}}
420+
{{$network_tag:=or (first (groupByKeys$containers"Env.NETWORK_ACCESS"))"external" }}
421+
419422
{{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect"*/}}
420423
{{$https_method:=or (first (groupByKeys$containers"Env.HTTPS_METHOD"))"redirect" }}
421424

@@ -442,6 +445,17 @@ server {
442445

443446
{{if$is_https }}
444447

448+
{{ifeq$https_method"redirect" }}
449+
server {
450+
server_name {{$host_alias }};
451+
listen 80 {{$default_server }};
452+
{{if$enable_ipv6 }}
453+
listen [::]:80 {{$default_server }};
454+
{{end }}
455+
access_log /var/log/nginx/access.log vhost;
456+
return 301 https://{{$first_host }}$request_uri;
457+
}
458+
{{end }}
445459
server {
446460
server_name {{$host_alias }};
447461
listen 443 ssl http2 {{$default_server }};
@@ -484,4 +498,44 @@ server {
484498

485499
{{end }}
486500

501+
{{ifor (not$is_https) (eq$https_method"noredirect") }}
502+
503+
server {
504+
server_name {{$host_alias }};
505+
listen 80 {{$default_server }};
506+
{{if$enable_ipv6 }}
507+
listen [::]:80 {{$default_server }};
508+
{{end }}
509+
access_log /var/log/nginx/access.log vhost;
510+
511+
{{ifeq$network_tag"internal" }}
512+
# Only allow traffic from internal clients
513+
include /etc/nginx/network_internal.conf;
514+
{{end }}
515+
516+
{{if (exists (printf"/etc/nginx/vhost.d/%s"$host_alias)) }}
517+
include {{printf"/etc/nginx/vhost.d/%s"$host_alias }};
518+
{{elseif (exists"/etc/nginx/vhost.d/default") }}
519+
include /etc/nginx/vhost.d/default;
520+
{{end }}
521+
522+
return 301 http://{{$first_host }}$request_uri;
523+
}
524+
525+
{{if (and (not$is_https) (exists"/etc/nginx/certs/default.crt") (exists"/etc/nginx/certs/default.key")) }}
526+
server {
527+
server_name {{$host_alias }};
528+
listen 443 ssl http2 {{$default_server }};
529+
{{if$enable_ipv6 }}
530+
listen [::]:443 ssl http2 {{$default_server }};
531+
{{end }}
532+
access_log /var/log/nginx/access.log vhost;
533+
return 500;
534+
535+
ssl_certificate /etc/nginx/certs/default.crt;
536+
ssl_certificate_key /etc/nginx/certs/default.key;
537+
}
538+
{{end }}
539+
540+
{{end }}
487541
{{end }}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp