sites-available
Nginx 站點配置文件
預設情況下,Nginx 使用 /etc/nginx/sites-available/default 作為預設站點配置。
我們可以依需求建立新的配置檔(例如 temp_api.conf),用來專門配置新的 API 反向代理與靜態資源服務。
同樣的,我們來看一下這個檔案的內容:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;
	root /var/www/html;
	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;
	server_name _;
	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}
設定檔概述
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
這部分是作者留下的「註解」,提供了一些學習 Nginx 設定的資源連結,並建議管理員將此文件留在 sites-available/ 內作為參考,實際運行的設定應從 sites-enabled/ 目錄載入。
既然是開發者留下的告誡,肯定有它的道理,所以我們不要動它,繼續看下去。
預設伺服器設定
server {
	listen 80 default_server;
	listen [::]:80 default_server;
- 
listen 80 default_server;- 伺服器監聽 80 埠(HTTP),並設定為「預設伺服器」(default server),當 
server_name無匹配時,請求將被此區塊處理。 
 - 伺服器監聽 80 埠(HTTP),並設定為「預設伺服器」(default server),當 
 - 
listen [::]:80 default_server;- 允許 IPv6 的請求也能透過 80 埠存取此伺服器。
 
 - 
預設網站根目錄與首頁設定
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;root /var/www/html;- 設定網站的根目錄,所有請求的靜態檔案(HTML、CSS、JS)將從此目錄提供。
 index index.html index.htm index.nginx-debian.html;- 設定預設首頁文件,若 
index.html存在則直接回應此檔案,否則尋找index.htm,若皆不存在則依location規則處理請求。 
 - 
伺服器名稱設定
server_name _;server_name _;_代表「任何未匹配的請求」都會由此伺服器區塊處理。- 可更改為特定域名,如 
server_name example.com;來讓該設定只適用於example.com。 
 - 
處理靜態資源與 404 頁面
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}location / {}- 設定 
/(根目錄)的請求處理方式。 try_files $uri $uri/ =404;- 依序嘗試:
$uri(請求的檔案,如/index.html)。$uri/(如果請求的路徑是目錄,則檢查目錄內是否有index.html)。- 若皆不存在,則返回 404 Not Found。
 
 
 
FastCGI PHP 處理
此程式區段預設為註解。
	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}
此區塊為 FastCGI 設定,讓 Nginx 處理 PHP 請求並轉交給 PHP-FPM 伺服器。
- 啟用 PHP-FPM 處理方式:
- 取消 
location ~ \.php$註解。 - 確保 PHP-FPM 運行在 
/run/php/php7.4-fpm.sock或127.0.0.1:9000。 
 - 取消 
 
限制存取 .htaccess
此程式區段預設為註解。
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
.htaccess 是 Apache 使用的設定檔,Nginx 不支援 .htaccess。
但若 Nginx 與 Apache 共存,可透過此規則防止 .htaccess 曝露給外部存取。
範例虛擬主機設定
此程式區段預設為註解。
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}
這是一個額外的虛擬主機範例,適用於 example.com 網站。
你可以將這段程式碼移到 sites-available/example.com,然後建立符號連結到 sites-enabled/ 來啟用。
如何啟用/停用此設定
完成 sites-available 內的配置檔案之後,要把這個檔案連結到 sites-enabled 才能生效。
以這個 default 設定檔為例,在 Ubuntu/Debian 系統中,我們使用以下指令來啟用:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
如果要停用的話,首先要刪除 sites-enabled 內的連結:
sudo rm /etc/nginx/sites-enabled/default
然後重新載入 Nginx:
sudo systemctl reload nginx
這個 default 設定檔通常作為 Nginx 預設網站,適合用來測試 Nginx 是否正常運行。
如果要自訂網站,這裡建議要「建立新設定檔」,並啟用適當的 server_name 設定。