# [Nginx] 設定 ###### tags: `server` `Nginx` `指令` `linux` ## 常用 ### 熱加載 重新加載配置檔,若失敗表示最新的配置檔有錯誤,會沿用原本的配置 `/etc/nginx/nginx.conf -s reload` ### 重啟 重新開啟 `/etc/init.d/nginx restart` ### 測試 `curl http://127.0.0.1/nginx_status` ## 設定檔位置 * 查看設定檔位置 `nginx -t` * 主要設定檔 `/etc/nginx/nginx.conf` * 其他設定檔 (可以從主要設定檔內include的路徑來查找) * `/etc/nginx/conf.d/` * `/etc/nginx/site-enable/` * `/etc/nginx/site-available/` ### nginx.conf 文件結構 ```=Vim #全域 events { # 網路連接相關設定 } http { # 多個server設定 日誌 請求設定等 server { # server內相關參數 location [PATTERN] { # 設定請求的路由與各頁面處理情形 } location [PATTERN] { # 設定請求的路由與各頁面處理情形 } } server { ... } } ``` ### 開源版 ```=linux=1 user nobody nobody; # 使用者名稱,使用 ps aux 查看 process會顯示這個名稱,預設為nobody nobody worker_processes auto; # Nginx的執行緒數量(建議為機器CPU數量x2) pid /run/nginx.pid; # Nginx的執行緒pid位址 include /etc/nginx/modules-enabled/*.conf; # 讀取 ./modules-enabled/ 內的設定檔 error_log log/error.log debug; # 自訂日誌路徑、級別,可以放在全域、http和server。種類有debug.info.notice.warn.error.crit.alert.emerg等。 # events 區塊,負責處理每個網路連接 events { worker_connections 768; # 每個執行緒同一時間允許的連線總數量 # multi_accept on; # 是否可以接受多個執行緒 accept_mutex on; #use epoll; } # http 區塊,負責處理透過http協定的連結 http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; # Access log 檔案存放位置 error_log /var/log/nginx/error.log; # log 檔案存放位置 ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } # http 區塊,負責處理透過mail協定的連結,目前註解掉了 #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #} ``` ### 商業版 ```=linux=1 user nginx; # 使用者名稱,使用 ps aux 查看 process會顯示這個名稱 worker_processes 1; # Nginx的執行緒數量(建議為機器CPU數量x2) error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; # 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; #這行會讀取到前面那個default.conf,表示/conf.d/default.conf是專門給http使用 --------------------------------------------- #設定load balancer,首先要設定server的group,放在upstream這個指標裡面,group名稱為'backend' upstream backend { server backend1.example.com weight=5; server backend2.example.com; server 192.0.0.1 backup; } --------------------------------------------- #slow_start參數:server重啟可能一瞬間收到大量的流量又掛掉一次,設定slow_start可以在設定時間內讓流量逐漸達到預設值。 upstream backend { server backend1.example.com slow_start=30s; server backend2.example.com; server 192.0.0.1 backup; } --------------------------------------------- } ``` ## 負載平衡 (Load balancing) * **Round Robin(default)** 除了預設外,還可以在IP/domain設定權重,依照權重分配 ```=vim http{ upstream myweb { server web1.dtask.idv.tw weight=3; server web2.dtask.idv.tw weight=2; } server { listen 80; location / { proxy_pass http://myweb; } } } ``` * **least-connected** 流量會導到連線數最少的server * **IP-hash** 會將IPv4的第一組八位元或是IPv6整串經過hash table對應到一台server,只要使用者從同一個IP來,流量就會固定對應到同一台server ### 查看負載狀況 啟用 Nginx 內建的 stub_status 模組,在網頁上顯示伺服器即時的負載狀況,方便管理者即時監控 ```=vim! location /nginx_status { stub_status on; #開啟 stub_status 模組 access_log off; allow xxx.xxx.xxx.xxx; allow 127.0.0.1; deny all; } ``` ## Virtual Host 設定 - `/bin/sbin` 放置系統管理員可以操作的指令 - `/bin` 主要放置一般使用者可以操作的指令 - `/boot` 開機相關 - `/dev` device 裝置檔案,如滑鼠.鍵盤等 - `/etc` 系統檔案 ## proxy 設定 假設有個後端伺服器開在8000端口,若我需要後端伺服器幫我處理 /apr/v1 開頭的請求路徑... ```=linux location /api/v1{ proxy_pass http://localhost:8000; } location / { root 路徑; index index.html; proxy_pass http://localhost:8080; } ``` 該例是把 後端開出的 8000 ,綁定回原本 80/443,所以網址會變成 `https://xxx.com/api/v1` 是讀 8000 的東西 假設後端開了兩版本api,分別是8000和8001,proxy_pass可以把8000 --> v1,8001 -->v2 比如 laravel 啟動serve會執行 php artisan serve ,這時候會把專案啟動為 `localhost:8000`。如果兩個laravel啟動,可以第二個改走 `localhost:8001` 同一台server不能都監聽同一個port,比如 react start 啟動過 3000 ,必須要再啟動第2個 react ,會幫你改成 3001 reverse proxy 和 rewrite ## :triangular_flag_on_post: 問題 文章底下有一些 nginx.conf 檔的相關設定 => [Nginx常見錯誤及處理方法](https://www.796t.com/content/1544238548.html) 當連線數量過多時,nginx.conf 可以調整的相關設定 => [Oracle报错ORA-12516 TNS:listener could not find available handler with matching protocol stack](https://blog.csdn.net/tianqishu11/article/details/78615850) --- 資料來源: [nginx.conf 配置檔案](https://netkiller.sourceforge.net/www/nginx/conf.html) [Nginx 學習筆記(含Dockerfile、proxy server)](https://medium.com/@stan5566/nginx-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-850147a74745) [常用的 Nginx Config 與相關指令教學](https://linyencheng.github.io/2019/07/13/tool-nginx/) [Nginx 教學](https://hi-founder.com/p/nginx-%E6%95%99%E5%AD%B8/) [Nginx docs](https://docs.nginx.com/nginx/admin-guide/monitoring/logging/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up