Config 檔案
Nginx的config配置檔案
- /etc/nginx/nginx.conf (主配置)
- /etc/nginx/conf.d/default.conf (預設會被加載的配置檔)
Nginx的預設log放置位置
- /var/log/nginx
Nginx的模塊目錄
- /etc/nginx/module
Nginx內建的html檔位址
- /usr/share/nginx/html
- /usr/share/nginx/html/index.html (nginx內建的wellcome頁面)
基本指令
若修改了config,需要執行指令才會以新的設定運作
- nginx -s reload
- nginx -s reload -c /etc/nginx/nginx.conf
在reload前可以用此指令檢查配置有無語法錯誤
- nginx -t
- nginx -T
- nginx -t -c /etc/nginx/nginx.conf
關閉Nginx
- nginx -s stop (fast shutdown)
- nginx -s quit (graceful shutdown)
Nginx 變數列表
官方文件 Alphabetical index of variables
在寫Config檔會用到這些變數
預設的Config檔
進入容器裡看了一下一開始預設的設定檔長怎樣
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
- user:用來指定以哪個用戶權限來跑 worker process (master process 會是以 root 權限執行)
- worker_processes:設定 nginx 跑的 worker process 數量
- events:配置連線設定
- http:設定 nginx 對 http 連線處理
- include:可以引入別的設定檔
- default_type mime-type:定義 response 預設的 MIME 類別
- log_format:定義 log 格式
- sendfile:是否使用 sendfile() 來傳輸文件
- keepalive_timeout:設定 Keep-Alive 的連線時間上限
- gzip on:開啟 gzip 壓縮網頁檔案 (https://nginx.org/en/docs/http/ngx_http_gzip_module.html)
/etc/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
- server區塊:定義了Nginx需要監聽的port與處理
- server_name:定義請求指向的hostname,像下面的例子,可以有多個server監聽同個port,Nginx會看request HTTP帶的hostname決定要派發處理的server,若是都沒有符合的server_aname,會選擇有標default_server的那個,如果沒標default_server則會預設選擇第一個定義的
server {
listen 80;
server_name www.example_1.com;
}
server {
listen 80 default_server;
server_name www.example_2.com;
}
server {
listen 80;
server_name www.example_3.com;
}
- location:定義請求 path 對應的回應
- root:設定靜態檔案資料夾路徑,以上例的default.conf來看的話,如果請求路徑是 /test.html,就會去找跑 Nginx 機器上的這個檔案路徑 /usr/share/nginx/html/test.html 回傳給用戶端
- index:設定請求路徑為 / 回的檔案
沒有留言:
張貼留言