準備在Mac 上部署Nginx,所以查閱了相關的知識,記錄一下。
Nginx 知識點:
-
Nginx 官網、源代碼
Nginx功能:
Http代理、反向代理
負載均行
web緩存
- 常用命令
brew install nginx 安裝nginx
brew ls nginx 查看nginx安裝路徑
nginx 啟動nginx 啟動成功之后在瀏覽器輸入localhost:8080即可查看(默認端口為8080)
brew services start nginx 啟動nginx
brew services stop nginx 關閉nginx
brew services resatrt nginx 重啟nginx
nginx -s quit 退出
nginx -s reload 重新加載
nginx -t 測試nginx.conf配置
/usr/local/etc/nginx/nginx.conf (配置文件路徑)
/usr/local/var/www (服務器默認路徑)
/usr/local/Cellar/nginx/x.x.x (安裝路徑)
Nginx配置
... #全局塊
events { #events塊
...
}
http #http塊
{
... #http全局塊
server #server塊
{
... #server全局塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局塊
}
- 全局塊:配置nginx全局參數(shù)。主要包括:運行nginx服務器的用戶組、nginx進程pid存放路徑、日志存放路徑、配置文件引入、允許生成worker process數(shù)等。
- event塊:配置nginx服務器或與用戶的網絡連接相關參數(shù)。包括:每個進程的最大連接數(shù)、事件驅動模型處理連接請求類型、是否允許同時接受多個網路連接、開啟多個網絡連接序列化等。
- http塊:配置代理、緩存、日志定義等絕大多數(shù)功能和第三方模塊。包括文件引入、mime-type定義、日志自定義、是否使用sendfile傳輸文件、連接超時時間、單連接請求數(shù)等。
- server塊:配置虛擬主機的相關參數(shù),一個http中可以有多個server,一個server多個location塊。
- location塊:配置請求的路由,以及不同狀態(tài)下各頁面的處理情況。
- ‘#’為注釋
示例配置:
########### 每個指令必須有分號結束。#################
#user administrator administrators; #配置用戶或者組,默認為nobody nobody。
#worker_processes 2; #允許生成的進程數(shù),默認為1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設置網路連接序列化,防止驚群現(xiàn)象發(fā)生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網絡連接,默認為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數(shù),默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個進程每次調用傳輸數(shù)量不能大于設定的值,默認為0,即不設上限。
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數(shù)。
listen 4545; #監(jiān)聽端口
server_name 127.0.0.1; #監(jiān)聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。
#root path; #根目錄
#index vv.txt; #設置默認頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
其中:
1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
2.$remote_user :用來記錄客戶端用戶名稱;
3.$time_local : 用來記錄訪問時間與時區(qū);
4.$request : 用來記錄請求的url與http協(xié)議;
5.$status : 用來記錄請求狀態(tài);成功是200,
6.$body_bytes_sent :記錄發(fā)送給客戶端文件主體內容大小;
7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
8.$http_user_agent :記錄客戶端瀏覽器的相關信息;
- 參考鏈接
https://www.cnblogs.com/knowledgesea/p/5175711.html