Nginx & Apache2 configuration of flutter website

Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Redirect all HTTP traffic to HTTPS
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;

# Logs for redirect traffic
access_log /var/log/nginx/example.redirect.access.log;
error_log /var/log/nginx/example.redirect.error.log;

return 301 https://example.com$request_uri;
}

# Redirect www to non‑www and serve HTTPS
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

# Logs for www→non‑www redirects
access_log /var/log/nginx/example.www-redirect.access.log;
error_log /var/log/nginx/example.www-redirect.error.log;

return 301 https://example.com$request_uri;
}

# Serve Flutter Web App with HTTPS
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

# Application logs
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;

root /home/sync-user/example/html;
index index.html;

location / {
try_files $uri $uri/ /index.html;
}

# Ensure proper MIME type for WebAssembly files
location ~ \.wasm$ {
types { application/wasm wasm; }
}

# Cache settings for static assets
location ~* \.(?:ico|css|js|gif|jpe?g|png|svg|woff2?|ttf|eot|otf)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}

Apache2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Redirect all HTTP traffic to HTTPS
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

# Log files for redirect
ErrorLog /var/log/apache2/example.redirect.error.log
CustomLog /var/log/apache2/example.redirect.access.log combined

# Redirect everything to https://example.com
RewriteEngine On
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>


# Redirect www → non‑www over HTTPS
<VirtualHost *:443>
ServerName www.example.com

# SSL certificates
SSLEngine on
SSLCertificateFile /home/sync-user/example/SSL/live/example.com/fullchain.pem
SSLCertificateKeyFile /home/sync-user/example/SSL/live/example.com/privkey.pem

# Log files
ErrorLog /var/log/apache2/example.www-redirect.error.log
CustomLog /var/log/apache2/example.www-redirect.access.log combined

# Redirect to non‑www
RewriteEngine On
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
</VirtualHost>


# Serve Flutter Web App with HTTPS
<VirtualHost *:443>
ServerName example.com

# SSL certificates
SSLEngine on
SSLCertificateFile /home/sync-user/example/SSL/live/example.com/fullchain.pem
SSLCertificateKeyFile /home/sync-user/example/SSL/live/example.com/privkey.pem

# Log files for app
ErrorLog /var/log/apache2/example.error.log
CustomLog /var/log/apache2/example.access.log combined

DocumentRoot /home/sync-user/example/html
DirectoryIndex index.html

<Directory /home/sync-user/example/html>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted

# Fallback routing for SPA
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
</Directory>

# Ensure proper MIME type for WebAssembly
AddType application/wasm .wasm

# Cache static assets for 30 days
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/x-icon "access plus 30 days"
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/svg+xml "access plus 30 days"
ExpiresByType font/woff2 "access plus 30 days"
ExpiresByType application/font-woff "access plus 30 days"
ExpiresByType application/vnd.ms-fontobject "access plus 30 days"
ExpiresByType font/ttf "access plus 30 days"
ExpiresByType font/otf "access plus 30 days"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(?:ico|css|js|gif|jpe?g|png|svg|woff2?|ttf|eot|otf)$">
Header set Cache-Control "public, immutable, max-age=2592000"
</FilesMatch>
</IfModule>
</VirtualHost>