Skip to content

Commit ae0df9c

Browse files
authored
Merge pull request #97 from LibreCodeCoop/feat/implement-https
feat: implement https
2 parents 2cc0deb + abe4677 commit ae0df9c

File tree

7 files changed

+179
-172
lines changed

7 files changed

+179
-172
lines changed

.docker/Dockerfile.nginx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
FROM nginx:alpine
22

3-
COPY nginx/nginx.conf /etc/nginx/nginx.conf
3+
RUN apk add --no-cache openssl
4+
5+
COPY nginx/nginx.conf /etc/nginx/nginx.conf
6+
COPY nginx/config/ /etc/nginx/conf.d/
7+
COPY scripts/nginx-entrypoint.sh /usr/local/bin/nginx-entrypoint.sh
8+
9+
RUN chmod +x /usr/local/bin/nginx-entrypoint.sh
10+
11+
ENTRYPOINT ["/usr/local/bin/nginx-entrypoint.sh"]

.docker/nginx/config/common.conf

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Common configuration for both HTTP and HTTPS
2+
3+
# Add headers to serve security related headers
4+
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload;" always;
5+
6+
# set max upload size
7+
client_max_body_size 20G;
8+
fastcgi_buffers 64 4K;
9+
10+
# Enable gzip but do not remove ETag headers
11+
gzip on;
12+
gzip_vary on;
13+
gzip_comp_level 4;
14+
gzip_min_length 256;
15+
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
16+
gzip_types application/atom+xml text/javascript application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/wasm application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
17+
18+
# The settings allows you to optimize the HTTP2 bandwidth
19+
client_body_buffer_size 512k;
20+
21+
# HTTP response headers borrowed from Nextcloud `.htaccess`
22+
add_header Referrer-Policy "no-referrer" always;
23+
add_header X-Content-Type-Options "nosniff" always;
24+
add_header X-Download-Options "noopen" always;
25+
add_header X-Frame-Options "SAMEORIGIN" always;
26+
add_header X-Permitted-Cross-Domain-Policies "none" always;
27+
add_header X-Robots-Tag "noindex,nofollow" always;
28+
add_header X-XSS-Protection "1; mode=block" always;
29+
30+
# Remove X-Powered-By, which is an information leak
31+
fastcgi_hide_header X-Powered-By;
32+
33+
# Path to the root of your installation
34+
root /var/www/html;
35+
36+
# Add .mjs as a file extension for javascript
37+
include mime.types;
38+
types {
39+
application/javascript js mjs;
40+
}
41+
42+
# Specify how to handle directories
43+
index index.php index.html /index.php$request_uri;
44+
45+
# Rule borrowed from `.htaccess` to handle Microsoft DAV clients
46+
location = / {
47+
if ( $http_user_agent ~ ^DavClnt ) {
48+
return 302 /remote.php/webdav/$is_args$args;
49+
}
50+
}
51+
52+
location = /robots.txt {
53+
allow all;
54+
log_not_found off;
55+
access_log off;
56+
}
57+
58+
# Make a regex exception for `/.well-known` so that clients can still
59+
# access it despite the existence of the regex rule
60+
location ^~ /.well-known {
61+
# The rules in this block are an adaptation of the rules
62+
# in `.htaccess` that concern `/.well-known`.
63+
64+
location = /.well-known/carddav { return 301 /remote.php/dav/; }
65+
location = /.well-known/caldav { return 301 /remote.php/dav/; }
66+
67+
location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
68+
location /.well-known/pki-validation { try_files $uri $uri/ =404; }
69+
70+
# Let Nextcloud's API for `/.well-known` URIs handle all other
71+
# requests by passing them to the front-end controller.
72+
return 301 /index.php$request_uri;
73+
}
74+
75+
# Rules borrowed from `.htaccess` to hide certain paths from clients
76+
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; }
77+
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; }
78+
79+
# Ensure this block, which passes PHP files to the PHP process, is above the blocks
80+
# which handle static assets (as seen below). If this block is not declared first,
81+
# then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
82+
# to the URI, resulting in a HTTP 500 error response.
83+
location ~ \.php(?:$|/) {
84+
# Required for legacy support
85+
rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|ocs-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;
86+
87+
fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
88+
set $path_info $fastcgi_path_info;
89+
try_files $fastcgi_script_name =404;
90+
91+
include fastcgi_params;
92+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
93+
fastcgi_param PATH_INFO $path_info;
94+
95+
fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice
96+
fastcgi_param front_controller_active true; # Enable pretty urls
97+
fastcgi_pass php-handler;
98+
99+
fastcgi_intercept_errors on;
100+
fastcgi_request_buffering off;
101+
102+
fastcgi_max_temp_file_size 0;
103+
}
104+
105+
# Serve static files
106+
location ~ \.(?:css|js|mjs|svg|gif|png|jpg|ico|wasm|tflite|map|ogg|flac)$ {
107+
try_files $uri /index.php$request_uri;
108+
add_header Cache-Control "public, max-age=15778463";
109+
access_log off; # Optional: Don't log access to assets
110+
111+
location ~ \.wasm$ {
112+
default_type application/wasm;
113+
}
114+
}
115+
116+
location ~ \.woff2?$ {
117+
try_files $uri /index.php$request_uri;
118+
expires 7d; # Cache-Control policy borrowed from `.htaccess`
119+
access_log off; # Optional: Don't log access to assets
120+
}
121+
122+
# Rule borrowed from `.htaccess`
123+
location /remote {
124+
return 301 /remote.php$request_uri;
125+
}
126+
127+
location / {
128+
try_files $uri $uri/ /index.php$request_uri;
129+
}

.docker/nginx/config/http.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
server {
2+
listen 80;
3+
include /etc/nginx/conf.d/includes/*.conf;
4+
5+
include /etc/nginx/conf.d/common.conf;
6+
}

.docker/nginx/config/https.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server {
2+
listen 443 ssl http2;
3+
http2_max_field_size 16k;
4+
http2_max_header_size 32k;
5+
6+
ssl_certificate /tmp/nextcloud.pem;
7+
ssl_certificate_key /tmp/nextcloud.pem;
8+
ssl_protocols TLSv1.2 TLSv1.3;
9+
ssl_ciphers HIGH:!aNULL:!MD5;
10+
11+
include /etc/nginx/conf.d/includes/*.conf;
12+
13+
include /etc/nginx/conf.d/common.conf;
14+
}

.docker/nginx/nginx.conf

Lines changed: 4 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ worker_processes auto;
33
error_log /var/log/nginx/error.log warn;
44
pid /var/run/nginx.pid;
55

6-
76
events {
87
worker_connections 1024;
98
}
109

1110
http {
1211
disable_symlinks off;
13-
# Prevent nginx HTTP Server Detection
1412
server_tokens off;
1513

1614
include /etc/nginx/mime.types;
@@ -23,181 +21,18 @@ http {
2321
access_log /var/log/nginx/access.log main;
2422

2523
sendfile on;
26-
#tcp_nopush on;
27-
2824
keepalive_timeout 65;
2925

3026
set_real_ip_from 10.0.0.0/8;
3127
set_real_ip_from 172.16.0.0/12;
3228
set_real_ip_from 192.168.0.0/16;
3329
real_ip_header X-Real-IP;
3430

35-
#gzip on;
36-
3731
upstream php-handler {
3832
server nextcloud:9000;
3933
}
4034

41-
server {
42-
listen 80;
43-
include /etc/nginx/conf.d/includes/*.conf;
44-
45-
# Add headers to serve security related headers
46-
# Before enabling Strict-Transport-Security headers please read into this
47-
# topic first.
48-
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload;" always;
49-
50-
# set max upload size
51-
client_max_body_size 20G;
52-
fastcgi_buffers 64 4K;
53-
54-
# Enable gzip but do not remove ETag headers
55-
gzip on;
56-
gzip_vary on;
57-
gzip_comp_level 4;
58-
gzip_min_length 256;
59-
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
60-
gzip_types application/atom+xml text/javascript application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/wasm application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
61-
62-
# Pagespeed is not supported by Nextcloud, so if your server is built
63-
# with the `ngx_pagespeed` module, uncomment this line to disable it.
64-
#pagespeed off;
65-
66-
# The settings allows you to optimize the HTTP2 bandwidth.
67-
# See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
68-
# for tuning hints
69-
client_body_buffer_size 512k;
70-
71-
# HSTS settings
72-
# WARNING: Only add the preload option once you read about
73-
# the consequences in https://hstspreload.org/. This option
74-
# will add the domain to a hardcoded list that is shipped
75-
# in all major browsers and getting removed from this list
76-
# could take several months.
77-
#add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
78-
79-
# HTTP response headers borrowed from Nextcloud `.htaccess`
80-
add_header Referrer-Policy "no-referrer" always;
81-
add_header X-Content-Type-Options "nosniff" always;
82-
add_header X-Download-Options "noopen" always;
83-
add_header X-Frame-Options "SAMEORIGIN" always;
84-
add_header X-Permitted-Cross-Domain-Policies "none" always;
85-
add_header X-Robots-Tag "noindex,nofollow" always;
86-
add_header X-XSS-Protection "1; mode=block" always;
87-
88-
# Remove X-Powered-By, which is an information leak
89-
fastcgi_hide_header X-Powered-By;
90-
91-
# Path to the root of your installation
92-
root /var/www/html;
93-
94-
# Add .mjs as a file extension for javascript
95-
# Either include it in the default mime.types list
96-
# or include you can include that list explicitly and add the file extension
97-
# only for Nextcloud like below:
98-
include mime.types;
99-
types {
100-
application/javascript js mjs;
101-
}
102-
103-
# Specify how to handle directories -- specifying `/index.php$request_uri`
104-
# here as the fallback means that Nginx always exhibits the desired behaviour
105-
# when a client requests a path that corresponds to a directory that exists
106-
# on the server. In particular, if that directory contains an index.php file,
107-
# that file is correctly served; if it doesn't, then the request is passed to
108-
# the front-end controller. This consistent behaviour means that we don't need
109-
# to specify custom rules for certain paths (e.g. images and other assets,
110-
# `/updater`, `/ocs-provider`), and thus
111-
# `try_files $uri $uri/ /index.php$request_uri`
112-
# always provides the desired behaviour.
113-
index index.php index.html /index.php$request_uri;
114-
115-
# Rule borrowed from `.htaccess` to handle Microsoft DAV clients
116-
location = / {
117-
if ( $http_user_agent ~ ^DavClnt ) {
118-
return 302 /remote.php/webdav/$is_args$args;
119-
}
120-
}
121-
122-
location = /robots.txt {
123-
allow all;
124-
log_not_found off;
125-
access_log off;
126-
}
127-
128-
# Make a regex exception for `/.well-known` so that clients can still
129-
# access it despite the existence of the regex rule
130-
# `location ~ /(\.|autotest|...)` which would otherwise handle requests
131-
# for `/.well-known`.
132-
location ^~ /.well-known {
133-
# The rules in this block are an adaptation of the rules
134-
# in `.htaccess` that concern `/.well-known`.
135-
136-
location = /.well-known/carddav { return 301 http://$host/remote.php/dav/; }
137-
location = /.well-known/caldav { return 301 http://$host/remote.php/dav/; }
138-
139-
location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
140-
location /.well-known/pki-validation { try_files $uri $uri/ =404; }
141-
142-
# Let Nextcloud's API for `/.well-known` URIs handle all other
143-
# requests by passing them to the front-end controller.
144-
return 301 /index.php$request_uri;
145-
}
146-
147-
# Rules borrowed from `.htaccess` to hide certain paths from clients
148-
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; }
149-
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; }
150-
151-
# Ensure this block, which passes PHP files to the PHP process, is above the blocks
152-
# which handle static assets (as seen below). If this block is not declared first,
153-
# then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
154-
# to the URI, resulting in a HTTP 500 error response.
155-
location ~ \.php(?:$|/) {
156-
# Required for legacy support
157-
rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|ocs-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;
158-
159-
fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
160-
set $path_info $fastcgi_path_info;
161-
try_files $fastcgi_script_name =404;
162-
163-
include fastcgi_params;
164-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
165-
fastcgi_param PATH_INFO $path_info;
166-
167-
fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice
168-
fastcgi_param front_controller_active true; # Enable pretty urls
169-
fastcgi_pass php-handler;
170-
171-
fastcgi_intercept_errors on;
172-
fastcgi_request_buffering off;
173-
174-
fastcgi_max_temp_file_size 0;
175-
}
176-
177-
# Serve static files
178-
location ~ \.(?:css|js|mjs|svg|gif|png|jpg|ico|wasm|tflite|map|ogg|flac)$ {
179-
try_files $uri /index.php$request_uri;
180-
add_header Cache-Control "public, max-age=15778463";
181-
access_log off; # Optional: Don't log access to assets
182-
183-
location ~ \.wasm$ {
184-
default_type application/wasm;
185-
}
186-
}
187-
188-
location ~ \.woff2?$ {
189-
try_files $uri /index.php$request_uri;
190-
expires 7d; # Cache-Control policy borrowed from `.htaccess`
191-
access_log off; # Optional: Don't log access to assets
192-
}
193-
194-
# Rule borrowed from `.htaccess`
195-
location /remote {
196-
return 301 /remote.php$request_uri;
197-
}
198-
199-
location / {
200-
try_files $uri $uri/ /index.php$request_uri;
201-
}
202-
}
203-
}
35+
# Load HTTP and HTTPS server configurations
36+
include /etc/nginx/conf.d/http.conf;
37+
include /etc/nginx/conf.d/https.conf;
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# Generate self-signed certificate if it doesn't exist
4+
if [ ! -f /tmp/nextcloud.pem ]; then
5+
echo "Generating self-signed certificate..."
6+
openssl req -new -x509 -days 365 -nodes \
7+
-subj "/C=BR/ST=State/L=City/O=Nextcloud/CN=localhost" \
8+
-out /tmp/nextcloud.pem -keyout /tmp/nextcloud.pem 2>/dev/null || true
9+
fi
10+
11+
# Execute nginx
12+
exec nginx -g "daemon off;"

0 commit comments

Comments
 (0)