/etc/nginx/snippets/site-assets-fix.conf
/etc/nginx/snippets/site-mediawiki.conf
/etc/nginx/snippets/site-phpbb.conf
/etc/nginx/snippets/site-wordpress.conf
分別如下
nano /etc/nginx/snippets/site-assets-fix.conf
Code: Select all | 複製程式碼
# =========================================================================
# 靜態資源與動態載入器
# =========================================================================
# 1. MediaWiki 核心動態載入器(load.php 必須走 PHP)
location = /load.php {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/mediawiki/load.php;
expires 7d;
}
# 2. 實體靜態檔案(JS/CSS/圖片等)
# 注意:這裡不設 add_header,讓主站的安全標頭正常傳遞
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|webp|woff|woff2|ttf|eot|mp4|mp3)$ {
valid_referers none blocked *.zine-tw.com zine-tw.com 123.241.40.195;
if ($invalid_referer) {
return 403;
}
root /var/www/html;
try_files $uri =404;
access_log off;
expires 30d;
}
# 3. 靜態資源找不到時的兜底路由
location @fallback_assets {
if ($request_uri ~* "^/wordpress/") {
rewrite ^ /wordpress/index.php last;
}
if ($request_uri ~* "^/phpBB3/") {
rewrite ^ /phpBB3/app.php last;
}
return 404;
}
# 靜態資源與動態載入器
# =========================================================================
# 1. MediaWiki 核心動態載入器(load.php 必須走 PHP)
location = /load.php {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/mediawiki/load.php;
expires 7d;
}
# 2. 實體靜態檔案(JS/CSS/圖片等)
# 注意:這裡不設 add_header,讓主站的安全標頭正常傳遞
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|webp|woff|woff2|ttf|eot|mp4|mp3)$ {
valid_referers none blocked *.zine-tw.com zine-tw.com 123.241.40.195;
if ($invalid_referer) {
return 403;
}
root /var/www/html;
try_files $uri =404;
access_log off;
expires 30d;
}
# 3. 靜態資源找不到時的兜底路由
location @fallback_assets {
if ($request_uri ~* "^/wordpress/") {
rewrite ^ /wordpress/index.php last;
}
if ($request_uri ~* "^/phpBB3/") {
rewrite ^ /phpBB3/app.php last;
}
return 404;
}
Code: Select all | 複製程式碼
# =========================================================================
# MediaWiki 路由
# $wgScriptPath = /mediawiki (實體目錄 /var/www/html/mediawiki)
# $wgArticlePath = /wiki/$1 (對外短網址)
# 舊 Debian 安裝為 /var/lib/mediawiki + /var/www/html/w,已棄用
# =========================================================================
# 根目錄 → wiki 首頁
location = / {
return 301 $scheme://$host/wiki/%E9%A6%96%E9%A0%81;
}
# -----------------------------------------------------------------
# 舊 Debian /w/ 路徑 → 永久導向 /wiki/
# -----------------------------------------------------------------
location = /w/index.php {
if ($arg_title) {
rewrite ^ $scheme://$host/wiki/$arg_title? permanent;
}
return 301 $scheme://$host/wiki/;
}
location ^~ /w/ {
rewrite ^/w/(?:index\.php/|wiki/)?(.+)$ $scheme://$host/wiki/$1 permanent;
}
# -----------------------------------------------------------------
# 靜態資源直接送(避免進 PHP)
# -----------------------------------------------------------------
location ~* ^/mediawiki/(resources|images)/.*\.(css|js|png|jpe?g|gif|svg|woff2?|ttf)$ {
root /var/www/html;
access_log off;
expires 30d;
add_header Cache-Control "public";
}
# -----------------------------------------------------------------
# 短網址進入點 (/wiki/頁面名稱)
# -----------------------------------------------------------------
location /wiki/ {
try_files $uri $uri/ @mediawiki;
}
location @mediawiki {
rewrite ^/wiki/(.*)$ /mediawiki/index.php?title=$1&$args last;
}
# -----------------------------------------------------------------
# API / REST / index 直接存取(根目錄別名)
# -----------------------------------------------------------------
location ~ ^/(api|rest|index)\.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/mediawiki$fastcgi_script_name;
}
# -----------------------------------------------------------------
# MediaWiki 實體目錄
# -----------------------------------------------------------------
location /mediawiki/ {
root /var/www/html;
index index.php;
try_files $uri $uri/ =404;
# 只允許白名單 PHP 入口執行
location ~ ^/mediawiki/(index|api|rest|load|opensearch|thumb|img_auth)\.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
}
# 其他 .php 一律拒絕(防止上傳後門被執行)
location ~ \.php$ {
deny all;
}
}
# MediaWiki 路由
# $wgScriptPath = /mediawiki (實體目錄 /var/www/html/mediawiki)
# $wgArticlePath = /wiki/$1 (對外短網址)
# 舊 Debian 安裝為 /var/lib/mediawiki + /var/www/html/w,已棄用
# =========================================================================
# 根目錄 → wiki 首頁
location = / {
return 301 $scheme://$host/wiki/%E9%A6%96%E9%A0%81;
}
# -----------------------------------------------------------------
# 舊 Debian /w/ 路徑 → 永久導向 /wiki/
# -----------------------------------------------------------------
location = /w/index.php {
if ($arg_title) {
rewrite ^ $scheme://$host/wiki/$arg_title? permanent;
}
return 301 $scheme://$host/wiki/;
}
location ^~ /w/ {
rewrite ^/w/(?:index\.php/|wiki/)?(.+)$ $scheme://$host/wiki/$1 permanent;
}
# -----------------------------------------------------------------
# 靜態資源直接送(避免進 PHP)
# -----------------------------------------------------------------
location ~* ^/mediawiki/(resources|images)/.*\.(css|js|png|jpe?g|gif|svg|woff2?|ttf)$ {
root /var/www/html;
access_log off;
expires 30d;
add_header Cache-Control "public";
}
# -----------------------------------------------------------------
# 短網址進入點 (/wiki/頁面名稱)
# -----------------------------------------------------------------
location /wiki/ {
try_files $uri $uri/ @mediawiki;
}
location @mediawiki {
rewrite ^/wiki/(.*)$ /mediawiki/index.php?title=$1&$args last;
}
# -----------------------------------------------------------------
# API / REST / index 直接存取(根目錄別名)
# -----------------------------------------------------------------
location ~ ^/(api|rest|index)\.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/mediawiki$fastcgi_script_name;
}
# -----------------------------------------------------------------
# MediaWiki 實體目錄
# -----------------------------------------------------------------
location /mediawiki/ {
root /var/www/html;
index index.php;
try_files $uri $uri/ =404;
# 只允許白名單 PHP 入口執行
location ~ ^/mediawiki/(index|api|rest|load|opensearch|thumb|img_auth)\.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
}
# 其他 .php 一律拒絕(防止上傳後門被執行)
location ~ \.php$ {
deny all;
}
}
Code: Select all | 複製程式碼
# =========================================================================
# phpBB 3.3 路由與安全
# =========================================================================
# ① 根目錄敏感檔案阻擋(防止有人直接存取 /config.php)
location ~ ^/(config\.php|common\.php) {
error_page 403 =404 /404_deadlink;
deny all;
}
# ② 舊 phpBB 專屬檔案永久導向新子目錄 /phpBB3/
location ~ ^/(viewforum|viewtopic|memberlist|posting|ucp|mcp|search|faq|cron)\.php$ {
return 301 /phpBB3/$1.php$is_args$args;
}
location ^~ /download/file.php {
return 301 /phpBB3/download/file.php$is_args$args;
}
# ③ 阻擋 phpBB 來源目錄下的 PHP 檔案
location ~* ^/(images|cache|includes)/.*\.(php|pl|py|sh|inc|tpl)$ {
deny all;
error_page 403 =404 /404_deadlink;
}
# ④ phpBB 3.3 核心路由
location ^~ /phpBB3/ {
index index.php index.html index.htm;
try_files $uri $uri/ @phpbb_rewrite_ssl;
# PHP 解析
location ~ \.php(/|$) {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_PROXY "";
}
# 子目錄敏感檔案阻擋
location ~ ^/phpBB3/(config\.php|common\.php|store/|cache/data_) {
deny all;
error_page 403 =404 /404_deadlink;
}
}
# ⑤ 路由重寫
location @phpbb_rewrite_ssl {
rewrite ^/phpBB3/(.*)$ /phpBB3/app.php/$1 last;
}
# phpBB 3.3 路由與安全
# =========================================================================
# ① 根目錄敏感檔案阻擋(防止有人直接存取 /config.php)
location ~ ^/(config\.php|common\.php) {
error_page 403 =404 /404_deadlink;
deny all;
}
# ② 舊 phpBB 專屬檔案永久導向新子目錄 /phpBB3/
location ~ ^/(viewforum|viewtopic|memberlist|posting|ucp|mcp|search|faq|cron)\.php$ {
return 301 /phpBB3/$1.php$is_args$args;
}
location ^~ /download/file.php {
return 301 /phpBB3/download/file.php$is_args$args;
}
# ③ 阻擋 phpBB 來源目錄下的 PHP 檔案
location ~* ^/(images|cache|includes)/.*\.(php|pl|py|sh|inc|tpl)$ {
deny all;
error_page 403 =404 /404_deadlink;
}
# ④ phpBB 3.3 核心路由
location ^~ /phpBB3/ {
index index.php index.html index.htm;
try_files $uri $uri/ @phpbb_rewrite_ssl;
# PHP 解析
location ~ \.php(/|$) {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_PROXY "";
}
# 子目錄敏感檔案阻擋
location ~ ^/phpBB3/(config\.php|common\.php|store/|cache/data_) {
deny all;
error_page 403 =404 /404_deadlink;
}
}
# ⑤ 路由重寫
location @phpbb_rewrite_ssl {
rewrite ^/phpBB3/(.*)$ /phpBB3/app.php/$1 last;
}
nano /etc/nginx/nginx.conf
Code: Select all | 複製程式碼
# =========================================================================
# 核心效能區段 (全域層級)
# =========================================================================
user www-data;
# 自動對接您的 12 核心 CPU
worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
# 提高每個進程能打開的檔案描述符上限(Workerman WebSocket 長連線非常需要)
worker_rlimit_nofile 65535;
events {
# 12 核環境下,總共可支援 12 * 4096 = 49,152 個同時連線
worker_connections 4096;
# 讓核心盡可能同時接收多個新連線
multi_accept on;
# Linux 環境下最推薦的高效並發模型
use epoll;
}
# =========================================================================
# HTTP 核心優化區段 (應用層級)
# =========================================================================
http {
##
# 基礎網路傳輸優化
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
server_tokens off; # 隱藏 Nginx 版本號,提升安全性
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# 超時時間配置 (兼顧網頁與 Workerman 長連線)
##
# 網頁瀏覽器的長連線維持時間,設 30 秒能快速釋放閒置連線,保護 15G 記憶體
keepalive_timeout 30;
# 允許單一長連線傳輸最多 500 個請求
keepalive_requests 500;
# 遮蔽沒反應的客戶端,釋放快取
reset_timedout_connection on;
client_body_timeout 15;
send_timeout 15;
##
# 快取與緩衝區優化 (關鍵:防止大型 Wiki 頁面與 WordPress 產生臨時硬碟讀寫)
##
# 配合您 PHP 允許 800M 上傳,將 Nginx 的限制也放大
client_max_body_size 855M;
client_body_buffer_size 128k;
# 放大標頭緩衝區,防止複雜網頁或多 Cookie 導致 400 Bad Request
client_header_buffer_size 4k;
large_client_header_buffers 4 16k;
##
# Gzip 壓縮優化 (大幅降低網頁傳輸頻寬,減輕 CPU 負擔)
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
# 設為 5 即可,太高(如 9)會瘋狂榨乾您的 CPU
gzip_comp_level 5;
gzip_min_length 256;
# 針對 CMS 論壇、Wiki 的各種靜態、動態資源進行壓縮
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/x-javascript
application/xml
application/xml+rss
application/json
image/svg+xml;
##
# 載入您的虛擬主機設定檔 (保持原樣)
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# 核心效能區段 (全域層級)
# =========================================================================
user www-data;
# 自動對接您的 12 核心 CPU
worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
# 提高每個進程能打開的檔案描述符上限(Workerman WebSocket 長連線非常需要)
worker_rlimit_nofile 65535;
events {
# 12 核環境下,總共可支援 12 * 4096 = 49,152 個同時連線
worker_connections 4096;
# 讓核心盡可能同時接收多個新連線
multi_accept on;
# Linux 環境下最推薦的高效並發模型
use epoll;
}
# =========================================================================
# HTTP 核心優化區段 (應用層級)
# =========================================================================
http {
##
# 基礎網路傳輸優化
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
server_tokens off; # 隱藏 Nginx 版本號,提升安全性
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# 超時時間配置 (兼顧網頁與 Workerman 長連線)
##
# 網頁瀏覽器的長連線維持時間,設 30 秒能快速釋放閒置連線,保護 15G 記憶體
keepalive_timeout 30;
# 允許單一長連線傳輸最多 500 個請求
keepalive_requests 500;
# 遮蔽沒反應的客戶端,釋放快取
reset_timedout_connection on;
client_body_timeout 15;
send_timeout 15;
##
# 快取與緩衝區優化 (關鍵:防止大型 Wiki 頁面與 WordPress 產生臨時硬碟讀寫)
##
# 配合您 PHP 允許 800M 上傳,將 Nginx 的限制也放大
client_max_body_size 855M;
client_body_buffer_size 128k;
# 放大標頭緩衝區,防止複雜網頁或多 Cookie 導致 400 Bad Request
client_header_buffer_size 4k;
large_client_header_buffers 4 16k;
##
# Gzip 壓縮優化 (大幅降低網頁傳輸頻寬,減輕 CPU 負擔)
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
# 設為 5 即可,太高(如 9)會瘋狂榨乾您的 CPU
gzip_comp_level 5;
gzip_min_length 256;
# 針對 CMS 論壇、Wiki 的各種靜態、動態資源進行壓縮
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/x-javascript
application/xml
application/xml+rss
application/json
image/svg+xml;
##
# 載入您的虛擬主機設定檔 (保持原樣)
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
nano /etc/nginx/sites-available/zine-tw
Code: Select all | 複製程式碼
# =========================================================================
# 區塊 1A: Port 80 網域名稱 (強制導向 HTTPS)
# =========================================================================
server {
listen 80;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://$host$request_uri;
}
location = /404_deadlink {
internal;
return 404;
}
}
# =========================================================================
# 區塊 1B: Port 80 IP 直連 (中國/牆內用戶通道)
# =========================================================================
server {
listen 80 default_server;
server_name 123.241.40.195;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
root /var/www/html;
index index.php index.html index.htm;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;
# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;
location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
# 載入各站路由
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;
# -----------------------------------------------------------------
# 聊天室主要路由
# -----------------------------------------------------------------
location = /chat {
return 301 /chat/;
}
location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}
# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
include snippets/fastcgi-php.conf;
client_max_body_size 10M;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略,fastcgi-php.conf 已包含
}
# 聊天室圖片瀏覽路由(IP 直連專用 — 解除防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;
}
# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}
location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}
location = /awstats {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}
location = /awstats/ {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}
location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;
location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}
# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}
# 其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /404_deadlink {
internal;
return 404;
}
} # ← 補上這個 }
# =========================================================================
# 區塊 2: HTTPS 主站 (server_name zine-tw.com)
# =========================================================================
server {
listen 443 ssl;
http2 on;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
root /var/www/html;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/zine-tw.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zine-tw.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'; connect-src 'self' https://www.zine-tw.com ws://www.zine-tw.com wss://www.zine-tw.com https://zine-tw.com ws://zine-tw.com wss://zine-tw.com http://123.241.40.195:8282 ws://123.241.40.195:8282;" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
# add_header Cross-Origin-Embedder-Policy "credentialless" always;
# add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;
# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}
location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}
location = /awstats {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}
location = /awstats/ {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}
location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;
location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}
# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;
location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}
# 聊天室主要路由
location = /chat {
return 301 /chat/;
}
location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}
# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
client_max_body_size 10M;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略
}
# 聊天室圖片瀏覽路由(HTTPS 主站,保留防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;
valid_referers none blocked server_names 123.241.40.195;
if ($invalid_referer) {
return 403;
}
}
# 核心引入:各站點規則
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;
# 根目錄其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /404_deadlink {
internal;
return 404;
}
}
# 區塊 1A: Port 80 網域名稱 (強制導向 HTTPS)
# =========================================================================
server {
listen 80;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
location /.well-known/acme-challenge/ {
root /var/www/html;
}
location / {
return 301 https://$host$request_uri;
}
location = /404_deadlink {
internal;
return 404;
}
}
# =========================================================================
# 區塊 1B: Port 80 IP 直連 (中國/牆內用戶通道)
# =========================================================================
server {
listen 80 default_server;
server_name 123.241.40.195;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
root /var/www/html;
index index.php index.html index.htm;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;
# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;
location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
# 載入各站路由
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;
# -----------------------------------------------------------------
# 聊天室主要路由
# -----------------------------------------------------------------
location = /chat {
return 301 /chat/;
}
location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}
# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
include snippets/fastcgi-php.conf;
client_max_body_size 10M;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略,fastcgi-php.conf 已包含
}
# 聊天室圖片瀏覽路由(IP 直連專用 — 解除防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;
}
# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}
location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}
location = /awstats {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}
location = /awstats/ {
return 301 http://$host/awstats/awstats.pl?config=zine-tw.com;
}
location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;
location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}
# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}
# 其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /404_deadlink {
internal;
return 404;
}
} # ← 補上這個 }
# =========================================================================
# 區塊 2: HTTPS 主站 (server_name zine-tw.com)
# =========================================================================
server {
listen 443 ssl;
http2 on;
server_name zine-tw.com www.zine-tw.com;
# ① 先擋惡意 IP
if ($bad_ip) { return 444; }
# ② 再擋 bot UA
set $should_block "";
if ($block_bot) {
set $should_block "Y";
}
if ($is_google = "yes") {
set $should_block "N"; # 如果是真·Google IP,強制洗成不阻擋
}
if ($should_block = "Y") {
return 444;
}
# ③ 速率限制
limit_req zone=botzone burst=20 nodelay;
# ④ 掃描器路徑靜默處理
location ~* /\.(git|svn|hg|bzr|env|aws|DS_Store) {
access_log off;
return 404;
}
location ~ /\.ht {
access_log off;
return 404;
}
location = /dns-query {
access_log off;
return 404;
}
root /var/www/html;
index index.php index.html index.htm;
ssl_certificate /etc/letsencrypt/live/zine-tw.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/zine-tw.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'; connect-src 'self' https://www.zine-tw.com ws://www.zine-tw.com wss://www.zine-tw.com https://zine-tw.com ws://zine-tw.com wss://zine-tw.com http://123.241.40.195:8282 ws://123.241.40.195:8282;" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
# add_header Cross-Origin-Embedder-Policy "credentialless" always;
# add_header Cross-Origin-Resource-Policy "same-origin" always;
add_header Access-Control-Allow-Origin "*" always;
add_header Cache-Control "public, no-transform" always;
# -----------------------------------------------------------------
# AWStats
# -----------------------------------------------------------------
location ^~ /awstats-icon/ {
alias /usr/share/awstats/icon/;
access_log off;
}
location ^~ /awstatscss/ {
alias /usr/share/awstats/wwwroot/css/;
access_log off;
}
location = /awstats {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}
location = /awstats/ {
return 301 $scheme://$host/awstats/awstats.pl?config=zine-tw.com;
}
location ^~ /awstats/ {
alias /usr/lib/cgi-bin/;
index awstats.pl;
location ~ \.pl$ {
include fastcgi_params;
fastcgi_pass unix:/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
fastcgi_param SCRIPT_NAME /awstats.pl;
fastcgi_param DOCUMENT_ROOT /usr/lib/cgi-bin;
fastcgi_param QUERY_STRING $query_string;
charset utf-8;
override_charset on;
gzip off;
sub_filter_once off;
sub_filter '</style>' '</style><link rel="stylesheet" href="/awstatscss/awstats.css">';
}
}
# -----------------------------------------------------------------
# phpMyAdmin(僅允許本機)
# -----------------------------------------------------------------
location ^~ /phpmyadmin {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
alias /var/www/html/phpmyadmin/;
index index.php index.html;
location ~ \.php$ {
allow 127.0.0.1;
deny all;
error_page 403 =404 /404_deadlink;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
# -----------------------------------------------------------------
# WebSocket 代理
# -----------------------------------------------------------------
location ^~ /wss {
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}
# 聊天室主要路由
location = /chat {
return 301 /chat/;
}
location ^~ /chat/ {
alias /var/www/my-chat/public/;
index index.html;
}
# 聊天室圖片上傳 PHP 路由
location = /chat/upload.php {
client_max_body_size 10M;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/my-chat/upload.php;
# include fastcgi_params; ← 已省略
}
# 聊天室圖片瀏覽路由(HTTPS 主站,保留防盜鏈)
location ^~ /chat/uploads/ {
alias /var/www/my-chat/uploads/;
access_log off;
expires 30d;
valid_referers none blocked server_names 123.241.40.195;
if ($invalid_referer) {
return 403;
}
}
# 核心引入:各站點規則
include snippets/site-mediawiki.conf;
include snippets/site-phpbb.conf;
include snippets/site-wordpress.conf;
include snippets/site-assets-fix.conf;
# 根目錄其餘 PHP 解析
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /404_deadlink {
internal;
return 404;
}
}
debian 升級 nginx 請到官方網頁
https://nginx.org/en/linux_packages.html#Debian
Code: Select all | 複製程式碼
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
Code: Select all | 複製程式碼
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Code: Select all | 複製程式碼
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
pub rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key <signing-key@nginx.com>
Code: Select all | 複製程式碼
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
https://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
https://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
Code: Select all | 複製程式碼
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
| sudo tee /etc/apt/preferences.d/99nginx
Code: Select all | 複製程式碼
sudo apt update
sudo apt install nginx
sudo apt install nginx