summaryrefslogtreecommitdiffhomepage
path: root/nginx/sites-available/vps
blob: dff1d7b792bad5844148769d1f7ef332634b82d6 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#map $token $api_client_name {
#    default "";
#
#    # CAUTION!! Change token and client name wisely
#    "XXXX" "client";
#}

# Gets the basename of the original request
map $request_uri $request_basename {
  ~/(?<captured_request_basename>[^/?]*)(?:\?|$) $captured_request_basename;
}

# Gets the basename of the current uri
map $uri $basename {
  ~/(?<captured_basename>[^/]*)$ $captured_basename;
}

server {
  server_name topo.tw www.topo.tw;

  listen 80;
  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/fullchain.cert;
  ssl_certificate_key /etc/nginx/ssl/cert.pem;

  # files
  root /srv/http;
  index index.html;
  autoindex on;
  autoindex_exact_size off;
  disable_symlinks off;

  # charset
  charset utf-8;
  charset_types *;
  override_charset on;

  # header
  proxy_set_header Host $host;
  add_header Cache-Control "no-cache" always;
  #add_header Cache-Control "max-age=604800";
  error_page 404 /404.html;
  error_log /var/log/nginx/error.log debug;
  #rewrite_log on;

  location ~ \.html$ {
    try_files $uri =404;
  }

  location ~ \.js$ {
    add_header Access-Control-Allow-Origin *;
  }

  location = /cgi/ {
    alias /srv/cgi/;
  }

  location ^~ /cgi/(.+) {
    gzip off;

    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/fcgiwrap.sock;
    fastcgi_param SCRIPT_FILENAME /srv/cgi/$1;
  }

  location ~ [^/]$ {
    try_files $uri @rewrite_no_slash;
  }

  location @rewrite_no_slash {
    add_header rewrite no_slash;
    rewrite ^(.+)$ $1.html permanent;
  }

  location ~ /$ {
    try_files $uri @rewrite_slash;
  }

  location @rewrite_slash {
    rewrite ^(.+)/$ $1.html permanent;
  }

  rewrite ^/posts$ /posts/ permanent;
  location = /posts/ {
    autoindex_format xml;
    xslt_string_param title "/posts/";
    xslt_stylesheet layout/simple.xslt;
    add_header Cache-Control "no-cache" always;
  }

  # This configuration allow you to upload/modify/delete file, for example:
  #   curl -X PUT -F file=@foo https://topo.tw/doc/bar
  location ^~ /doc {
    alias /home/pham/doc/;

    client_body_temp_path /tmp/client_temp;
    dav_methods PUT DELETE MKCOL COPY MOVE;
    create_full_put_path  on;
    dav_access            group:rw  all:r;
    client_max_body_size 10000m;
  }

  location ^~ /photos/ {
    alias /home/pham/data/s3.photos/;
    autoindex_format xml;
    xslt_string_param title "photos";
    xslt_stylesheet layout/gal.xslt;
    try_files $uri $uri/ =404;
    expires max;

    if ($uri ~ ^/photos/([^!]+)!(large|lg|md)$ ) {
      set $filename /home/pham/data/s3.photos/$1;
      set $img_version $2;
      rewrite ^ /thumbnail;
    }
  }

  rewrite ^/p(ublic)?$ /public/ permanent;
  rewrite ^/p/(.*)$ /public/$1;
  location ^~ /public/ {
    alias /home/pham/public/;
    autoindex_format xml;
    xslt_string_param title "/posts/";
    xslt_stylesheet layout/simple.xslt;
    add_header Access-Control-Allow-Origin "*" always;
    add_header Cache-Control "no-cache" always;
  }

  # thumbnail CGI, requires variables 'filename' and 'img_version'
  location = /thumbnail {
    # Prepare the required parameters (width, height, cropping or zooming) according to the URL address!
    set $img_type resize;
    set $img_w    -;
    set $img_h    -;
    if ($img_version = 'large') {
      set $img_type resize;
      set $img_w    1920;
    }
    if ($img_version = 'lg') {
      set $img_type crop;
      set $img_w    256;
      set $img_h    256;
    }
    if ($img_version = 'md') {
      set $img_type crop;
      set $img_w    128;
      set $img_h    128;
    }
    rewrite ^ /_$img_type;
  }

  # Processing of Scaled Pictures
  location = /_resize {
    alias $filename;
    image_filter resize $img_w $img_h;
    image_filter_jpeg_quality 95;
    image_filter_buffer         20M;
    image_filter_interlace      on;
  }

  # Processing of clipped pictures
  location = /_crop {
    alias $filename;
    image_filter crop $img_w $img_h;
    image_filter_jpeg_quality 95;
    image_filter_buffer         20M;
    image_filter_interlace      on;
  }

  location ^~ /wallpapers {
    alias /home/pham/public/wallpapers/;

    autoindex_format xml;
    xslt_string_param title "Wallpaper Collection!";
    xslt_stylesheet layout/gal.xslt;
    try_files $uri $uri/ =404;

    if ($uri ~ ([^/!]+)!(large|lg|md)$ ) {
      set $filename /home/pham/public/wallpapers/$1;
      set $img_version $2;
      rewrite ^ /thumbnail;
    }
  }

  location ^~ /tmp {
    alias /home/pham/public/tmp/;
    autoindex on;
    add_header "Content-Type" "text/plain; charset=utf-8";
  }

  location ^~ /osm {
    alias /home/pham/public/osm/;
    autoindex on;
  }

  location ^~ /tainan/ {
    alias /home/pham/public/tainan/;
    autoindex on;
    index =404;
  }

  location /public/layx {
    alias /home/pham/public/layx/;

    autoindex on;

    auth_basic "You need to login";
    auth_basic_user_file /etc/nginx/passwd/2022.10.11;
  }

  location ^~ /private/ {
    alias /home/pham/private/;
    autoindex on;
    auth_basic "You need to login";
    auth_basic_user_file /etc/nginx/passwd/japan;
  }

  location = /japan.html {
    autoindex on;

    auth_basic "You need to login";
    auth_basic_user_file /etc/nginx/passwd/japan;
  }

  location ^~ /houshou {
    alias /home/pham/houshou/;

    autoindex on;
    autoindex_format xml;
    xslt_string_param title "Houshou Collection!";
    xslt_stylesheet layout/gal.xslt;
    try_files $uri $uri/ =404;

    auth_basic "You need to login";
    auth_basic_user_file /etc/nginx/passwd/houshou;

    if ($uri ~ ([^/!]+)!(large|lg|md)$ ) {
      set $filename /home/pham/houshou/$1;
      set $img_version $2;
      rewrite ^ /thumbnail;
    }
  }

  location ^~ /houshou2 {
    alias /home/pham/houshou2/;

    autoindex on;
    autoindex_format xml;
    xslt_string_param title "Houshou Collection!";
    #xslt_stylesheet layout/simple-gal.xslt;
    xslt_stylesheet layout/gal.xslt;
    try_files $uri $uri/ =404;

    if ($uri ~ ([^/!]+)!(large|lg|md)$ ) {
      set $filename /home/pham/houshou2/$1;
      set $img_version $2;
      rewrite ^ /thumbnail;
    }
  }

  location /rescue {
    root /home/pham;

    auth_basic "You need to login";
    auth_basic_user_file /etc/nginx/passwd/rescue;
  }

#  location /upload/ {
#    proxy_pass http://127.0.0.1:8000/;
#  }

  #rewrite ^/up$ /up/;
  #location /up/ {
    #    proxy_set_header X-Forwarded-Proto https;
    #    proxy_set_header Referer $host/up/;
    #    proxy_pass http://127.0.0.1:8080/;

    #    client_max_body_size 1g;
    #}

  #location = /_validate_token {
    #    internal;

    #    if ($token = "") {
      #        return 401; # Unauthorized
      #    }

    #    if ($api_client_name = "") {
      #        return 403; # Forbidden
      #    }

    #    return 204; # OK (no content)
    #}

}

# git server
server {
  server_name git.topo.tw;

  listen 80;
  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/git.topo.tw/fullchain.cer;
  ssl_certificate_key /etc/nginx/ssl/git.topo.tw/git.topo.tw.key;

  root /srv/git/www;

  location ~ (/.*) {
    #include       fastcgi_params;
    #fastcgi_param SCRIPT_FILENAME     /usr/lib/git-core/git-http-backend;
    ## export all repositories under GIT_PROJECT_ROOT
    #fastcgi_param GIT_HTTP_EXPORT_ALL "";
    #fastcgi_param GIT_PROJECT_ROOT    /srv/git;
    #fastcgi_param PATH_INFO           $1;
  }
}

# Block all direct accesses via IP address
server {
  server_name "~[\d\.]+";
  listen 80;
  error_page 404 /404.html;
  return 404;
}

## Redirect 80 to 443
#server {
#  if ($host = topo.tw) {
#    return 301 https://$host$request_uri;
#  } # managed by Certbot
#
#
#  #listen 80;
#  server_name topo.tw;
#  return 301 https://$host$request_uri;
#
#
#}

## Forward Proxy
#server {
#  resolver 8.8.8.8;
#  listen 13288;
#
#  proxy_connect;
#  proxy_connect_allow            443 563;
#  proxy_connect_connect_timeout  10s;
#  proxy_connect_read_timeout     10s;
#  proxy_connect_send_timeout     10s;
#  location / {
#    proxy_pass http://$host;
#    proxy_set_header Host $host;
#  }
#}

#server {
#  server_name demo.topo.tw;
#  root /;
#
#  location / {
#    index /home/pham/git/vps/demo/client.sh;
#    add_header Content-Type text/plain;
#  }
#  #listen 80;
#}