|
1 | | -FROM nginx:alpine |
2 | | -RUN apk add --no-cache curl |
3 | | -RUN rm /etc/nginx/conf.d/default.conf |
| 1 | +# ===================================================================================== |
| 2 | +# STAGE 1: The "Builder" |
| 3 | +# This stage uses Node.js to download the source files and build the static site. |
| 4 | +# ===================================================================================== |
| 5 | +FROM node:18-alpine AS builder |
4 | 6 |
|
5 | | -COPY <<EOF /etc/nginx/conf.d/gitbook-proxy.conf |
6 | | -upstream gitbook { |
7 | | -server nebulaclient.gitbook.io:443; |
8 | | -} |
| 7 | +# Install git, which is required by some tools. |
| 8 | +RUN apk add --no-cache git |
9 | 9 |
|
10 | | -server { |
11 | | - listen 3587; |
12 | | - server_name _; |
| 10 | +# Install the necessary command-line tools globally. |
| 11 | +# gitbook-exporter: Downloads the Markdown source from a live GitBook site. |
| 12 | +# gitbook-cli: The official tool to build a static site from Markdown source. |
| 13 | +RUN npm install -g gitbook-exporter gitbook-cli |
13 | 14 |
|
14 | | - resolver 8.8.8.8 valid=300s; |
15 | | - resolver_timeout 5s; |
| 15 | +# Set a working directory inside the container. |
| 16 | +WORKDIR /app |
16 | 17 |
|
17 | | - location = /robots.txt { |
18 | | - rewrite ^ /nebula/robots.txt last; |
19 | | - } |
| 18 | +# Run the exporter to download the source files from the live GitBook site. |
| 19 | +# This creates a directory named after the book, e.g., "nebula". |
| 20 | +RUN gitbook-exporter https://nebulaclient.gitbook.io/nebula/ |
20 | 21 |
|
21 | | - location ~ /(sitemap\.xml|robots\.txt)$ { |
22 | | - proxy_pass https://nebulaclient.gitbook.io; |
23 | | - proxy_ssl_server_name on; |
24 | | - proxy_ssl_verify off; |
| 22 | +# Navigate into the newly created source directory. |
| 23 | +# The directory name is based on the book's title, so we use a wildcard. |
| 24 | +WORKDIR /app/nebula |
25 | 25 |
|
26 | | - proxy_set_header Accept-Encoding ""; |
| 26 | +# Install the required plugins for the book (defined in its book.json). |
| 27 | +RUN gitbook install |
27 | 28 |
|
28 | | - proxy_set_header Host nebulaclient.gitbook.io; |
29 | | - proxy_set_header X-Real-IP \$remote_addr; |
30 | | - proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
31 | | - proxy_set_header X-Forwarded-Proto https; |
| 29 | +# Build the final, static HTML site. |
| 30 | +# This creates a "_book" directory containing all the files. |
| 31 | +RUN gitbook build |
32 | 32 |
|
33 | | - sub_filter_once off; |
34 | | - sub_filter_types text/xml application/xml text/plain; |
35 | | - sub_filter 'https://nebulaclient.gitbook.io' 'https://\$host'; |
36 | | - } |
| 33 | +# ===================================================================================== |
| 34 | +# STAGE 2: The "Runner" |
| 35 | +# This stage uses a minimal NGINX server to host the static files. |
| 36 | +# ===================================================================================== |
| 37 | +FROM nginx:1.25-alpine |
37 | 38 |
|
38 | | - location ~* \.(json|js|txt|xml)$ { |
39 | | - proxy_pass https://nebulaclient.gitbook.io; |
40 | | - proxy_ssl_server_name on; |
41 | | - proxy_ssl_verify off; |
42 | | - |
43 | | - proxy_set_header Host nebulaclient.gitbook.io; |
44 | | - proxy_set_header X-Real-IP \$remote_addr; |
45 | | - proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
46 | | - proxy_set_header X-Forwarded-Proto https; |
47 | | - proxy_set_header X-Forwarded-Host \$host; |
48 | | - proxy_set_header User-Agent \$http_user_agent; |
49 | | - |
50 | | - sub_filter_once off; |
51 | | - sub_filter_types application/xml application/json application/javascript text/javascript text/plain; |
52 | | - sub_filter 'https://nebulaclient.gitbook.io' 'https://\$host'; |
53 | | - sub_filter 'http://nebulaclient.gitbook.io' 'https://\$host'; |
54 | | - sub_filter 'nebulaclient.gitbook.io' '\$host'; |
55 | | - sub_filter '"//nebulaclient.gitbook.io' '"//\$host'; |
56 | | - sub_filter "'//nebulaclient.gitbook.io" "'//\$host"; |
57 | | - sub_filter "https:\\/\\/nebulaclient.gitbook.io" "https:\\/\\/\$host"; |
58 | | - sub_filter "nebulaclient\\.gitbook\\.io" "\$host"; |
59 | | - } |
| 39 | +# Set the port you want to expose. |
| 40 | +ARG PORT=3587 |
| 41 | +EXPOSE ${PORT} |
60 | 42 |
|
61 | | - location / { |
62 | | - proxy_pass https://nebulaclient.gitbook.io; |
63 | | - proxy_ssl_server_name on; |
64 | | - proxy_ssl_verify off; |
65 | | - |
66 | | - proxy_set_header Host nebulaclient.gitbook.io; |
67 | | - proxy_set_header X-Real-IP \$remote_addr; |
68 | | - proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; |
69 | | - proxy_set_header X-Forwarded-Proto https; |
70 | | - proxy_set_header X-Forwarded-Host \$host; |
71 | | - proxy_set_header X-Forwarded-Port \$server_port; |
72 | | - proxy_set_header User-Agent \$http_user_agent; |
73 | | - |
74 | | - proxy_redirect https://nebulaclient.gitbook.io/ https://\$host/; |
75 | | - proxy_redirect https://nebulaclient.gitbook.io https://\$host; |
76 | | - proxy_redirect http://nebulaclient.gitbook.io/ https://\$host/; |
77 | | - proxy_redirect http://nebulaclient.gitbook.io https://\$host; |
78 | | - proxy_redirect ~^https?://nebulaclient\.gitbook\.io(.*)\$ https://\$host\$1; |
79 | | - |
80 | | - proxy_connect_timeout 30s; |
81 | | - proxy_send_timeout 30s; |
82 | | - proxy_read_timeout 30s; |
83 | | - |
84 | | - proxy_http_version 1.1; |
85 | | - proxy_set_header Connection ""; |
86 | | - |
87 | | - sub_filter_once off; |
88 | | - sub_filter_types application/xml text/plain text/html text/css text/javascript application/javascript application/json text/xml application/xml; |
89 | | - |
90 | | - sub_filter 'https://nebulaclient.gitbook.io' 'https://\$host'; |
91 | | - sub_filter 'http://nebulaclient.gitbook.io' 'https://\$host'; |
92 | | - sub_filter 'nebulaclient.gitbook.io' '\$host'; |
93 | | - sub_filter '"//nebulaclient.gitbook.io' '"//\$host'; |
94 | | - sub_filter "'//nebulaclient.gitbook.io" "'//\$host"; |
95 | | - sub_filter "https:\\/\\/nebulaclient.gitbook.io" "https:\\/\\/\$host"; |
96 | | - sub_filter "http:\\/\\/nebulaclient.gitbook.io" "https:\\/\\/\$host"; |
97 | | - sub_filter "nebulaclient\\.gitbook\\.io" "\$host"; |
98 | | - |
99 | | - proxy_intercept_errors on; |
100 | | - error_page 301 302 307 = @handle_redirect; |
101 | | - } |
| 43 | +# Remove the default NGINX configuration file. |
| 44 | +RUN rm /etc/nginx/conf.d/default.conf |
| 45 | + |
| 46 | +# Copy the static HTML files generated in the "builder" stage |
| 47 | +# into the default NGINX web root directory. |
| 48 | +COPY --from=builder /app/nebula/_book /usr/share/nginx/html |
| 49 | + |
| 50 | +# Copy our custom NGINX configuration. |
| 51 | +# This config tells NGINX to listen on our chosen port and serve the static files. |
| 52 | +COPY <<EOF /etc/nginx/conf.d/gitbook.conf |
| 53 | +server { |
| 54 | + listen ${PORT}; |
| 55 | + server_name _; |
102 | 56 |
|
103 | | - location @handle_redirect { |
104 | | - set \$redirect_location \$upstream_http_location; |
105 | | - set \$redirect_host ''; |
106 | | - |
107 | | - if (\$redirect_location ~ "^https?://nebulaclient\.gitbook\.io(.*)") { |
108 | | - set \$redirect_path \$1; |
109 | | - return 302 https://\$host\$redirect_path; |
110 | | - } |
111 | | - |
112 | | - return 302 https://\$host/; |
| 57 | + # The root directory where our static files are located. |
| 58 | + root /usr/share/nginx/html; |
| 59 | + index index.html index.htm; |
| 60 | + |
| 61 | + # Standard configuration for serving static files. |
| 62 | + # It tries to find a file matching the URI, then a directory, |
| 63 | + # and finally falls back to the index.html for clean URLs. |
| 64 | + location / { |
| 65 | + try_files \$uri \$uri/ /index.html; |
113 | 66 | } |
114 | 67 |
|
115 | | - access_log /var/log/nginx/access.log; |
116 | | - error_log /var/log/nginx/error.log; |
| 68 | + # Optional: Add headers for security and caching. |
| 69 | + add_header X-Frame-Options "SAMEORIGIN"; |
| 70 | + add_header X-Content-Type-Options "nosniff"; |
| 71 | + add_header X-XSS-Protection "1; mode=block"; |
117 | 72 | } |
118 | 73 | EOF |
119 | 74 |
|
120 | | -EXPOSE 3587 |
| 75 | +# Command to run NGINX in the foreground. |
121 | 76 | CMD ["nginx", "-g", "daemon off;"] |
0 commit comments