nginx啟動(dòng)命令和默認(rèn)配置文件的使用
更新時(shí)間:2025年06月16日 14:07:51 作者:Gen鄧艮艮
這篇文章主要介紹了nginx啟動(dòng)命令和默認(rèn)配置文件的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
常見(jiàn)命令
# 默認(rèn)配置文件啟動(dòng) ./nginx # 停止 ./nginx -s stop # 重啟,加載默認(rèn)配置文件 ./nginx -s reload # 啟動(dòng)指定某個(gè)配置文件 ./nginx -c /usr/local/nginx/conf/nginx.conf
nginx.conf配置文件
#user nobody; # 指定Nginx worker進(jìn)程運(yùn)行以及用戶(hù)組 worker_processes 1; #error_log logs/error.log; # 錯(cuò)誤日志的存放路徑 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # 進(jìn)程pid存放路徑 # 事件模塊指令,用來(lái)指定Nginx的IO模型,Nginx支持的有select、poll、kqueue、epoll等。不同的是epoll用在Linux平臺(tái)上,而kqueue用在BSD系統(tǒng)中,對(duì)于Linux系統(tǒng),epoll工作模式是首選 events { use epoll; # 定義Nginx每個(gè)進(jìn)程的最大連接數(shù),作為服務(wù)器來(lái)說(shuō):worker_connections * worker_processes;作為反向代理來(lái)說(shuō),最大并發(fā)數(shù)量為:worker_connections * worker_processes / 2,因?yàn)榉聪虼矸?wù)器,每個(gè)并發(fā)會(huì)建立與客戶(hù)端的連接和與后端服務(wù)的連接,會(huì)占用兩個(gè)連接 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; # 自定義服務(wù)日志 #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 logs/access.log main; # 是否開(kāi)啟高效傳輸模式 on開(kāi)啟 off關(guān)閉 sendfile on; # 減少網(wǎng)絡(luò)報(bào)文段的數(shù)量 #tcp_nopush on; # 客戶(hù)端連接超時(shí)時(shí)間 #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # 虛擬主機(jī)的配置 server { listen 80; # 虛擬主機(jī)的服務(wù)端口 server_name localhost; # 用來(lái)指定IP地址或域名,多個(gè)域名之間用空格分開(kāi) #charset koi8-r; #access_log logs/host.access.log main; # URL地址匹配 location / { root html; # 服務(wù)默認(rèn)啟動(dòng)目錄 index index.html index.htm; # 默認(rèn)訪問(wèn)文件,按照順序找 } #error_page 404 /404.html; # 錯(cuò)誤狀態(tài)碼的顯示頁(yè)面 # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root 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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
location匹配規(guī)則
正則
- ^:開(kāi)始符號(hào)
- $:結(jié)束符號(hào)
location路徑匹配
- location = /uri:=表示精準(zhǔn)匹配,只有完全匹配上才能生效
- location /uri:不帶任何修飾符,表示前綴匹配
- location ^~ /uri:匹配任何以/uri開(kāi)頭的匹配到就停止搜索
- location /:通用匹配
正則匹配
- 區(qū)分大小寫(xiě)匹配:~
- 不區(qū)分大小寫(xiě)匹配:~*
圖片服務(wù)器
server { listen 80; server_name localhost a.com; location / { root html; index index.html index.htm; } location /img { alias /usr/local/img/; } }
注意
- 在location / 中配置root目錄
- 在location /path 中配置alias虛擬目錄,目錄后面的"/"符號(hào)一定要帶上
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx+SSL實(shí)現(xiàn)雙向認(rèn)證的示例代碼
這篇文章主要介紹了Nginx+SSL實(shí)現(xiàn)雙向認(rèn)證的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Nginx 安裝筆記(含PHP支持、虛擬主機(jī)、反向代理負(fù)載均衡)
Nginx安裝簡(jiǎn)記(含PHP支持、虛擬主機(jī)、反向代理負(fù)載均衡) Nginx,據(jù)說(shuō)高性能和穩(wěn)定性比Apache還牛,并發(fā)連接處理能力強(qiáng),低系統(tǒng)資源消耗。目前已有250多萬(wàn)web站點(diǎn)在使用2009-10-10Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn)
Nginx請(qǐng)求超時(shí)是服務(wù)器無(wú)法在規(guī)定時(shí)間內(nèi)完成對(duì)客戶(hù)端請(qǐng)求的響應(yīng),本文就來(lái)介紹一下Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02windows查看nginx是否啟動(dòng)及常用命令小結(jié)
這篇文章主要給大家介紹了關(guān)于windows查看nginx是否啟動(dòng)及常用命令的相關(guān)資料,在Windows系統(tǒng)中,可以使用以下命令來(lái)操作和管理Nginx,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06網(wǎng)頁(yè)502?Bad?Gateway?nginx/1.20.1報(bào)錯(cuò)的原因與解決方法
502 bad gateway nginx/1.20.1 是一個(gè)錯(cuò)誤提示,通常出現(xiàn)在訪問(wèn)網(wǎng)站時(shí)出現(xiàn)問(wèn)題,這篇文章主要給大家介紹了關(guān)于網(wǎng)頁(yè)502?Bad?Gateway?nginx/1.20.1報(bào)錯(cuò)的原因與解決方法,需要的朋友可以參考下2024-03-03