Nginx("engine x")是一款是由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。在高鏈接併發的狀況下,Nginx是Apache服務器不錯的替代品。
nginx的出現能夠說對於那些在windows上使用IIS,linux上使用apache2的人提供了更多的選擇,使用nginx的狀況主要是知足瞭如下的一些功能:php
本文主要是介紹了一些基礎的nginx的使用,環境是mac10.13.2。html
在mac上可使用兩種方法來進行:前端
本文沒有嘗試./configure make make install
的方式,不過能夠看看這個安裝NGINX;本文只是嘗試使用brew
來進行安裝。linux
homebrew主要分兩部分:git repo(位於GitHub)和二進制bottles(位於bintray),這二者在國內訪問都不太順暢。能夠替換成國內的鏡像,nginx
替換git源:git
替換brew.git: cd "$(brew --repo)" git remote set-url origin https://mirrors.ustc.edu.cn/brew.git 替換homebrew-core.git: cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
替換二進制bottles源[bash和zsh須要區分啓動文件]:web
//對於bash用戶: echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile source ~/.bash_profile //對於zsh用戶 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc source ~/.zshrc
如此即可以執行安裝:apache
brew install nginx
執行完成的話那麼就能夠查看結果如何:nginx -h
或者nginx -v
看看結果windows
nginx -h nginx version: nginx/1.12.2 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -T : test configuration, dump it and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/Cellar/nginx/1.12.2_1/) -c filename : set configuration file (default: /usr/local/etc/nginx/nginx.conf) -g directives : set global directives out of configuration file nginx -v nginx version: nginx/1.12.2
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx
nginx
的命令比較的少,大部分配置都是在配置文件當作,配置文件的路徑/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx
之中,#
能夠做爲註釋符來註釋掉改行;api
下面對這裏的部分配置內容作個總體的瞭解和分類:
從形式上分類:簡單指令和塊級指令集
配置文件中主要是存在一些simple directives and block directives
;能夠認爲是簡單的指令和塊級指令集,簡單指令就是:
worker_processes 1;
塊級指令集就是一個塊級指令名加上{}
,裏面包含不少簡單指令集,塊級指令集能夠嵌套;
events { worker_connections 1024; }
從功能模塊分類:主模塊、事件模塊、其餘基本模塊
nginx
的一些基本指令集合,包含了相似上述的簡單指令worker_processes 1;
在內的一些基本指令;http
模塊;先看一個初始狀態的配置文件:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root 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 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; # } #} include servers/*; }
開始分析配置文件中的一些指令:
在默認的生成的配置文件的頭部,有這麼幾行簡單的指令,雖然大部分是被註釋掉的,可是這裏簡單的說下其中的意義,這些簡單指令都屬於主模塊的指令,用於控制基本的nginx
的功能:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;
user
這個指令名錶明的是執行worker processes
的本機用戶,默認是nobody
,那麼若是須要讀寫一些roort
或者其餘用戶全部權的文件時,若是當前配置文件填寫的user
這個指令名對應的用戶又不具備r+w+x
的權限時,就會出現一些權限問題;
語法: user user [group]
缺省值: nobody nobody
指定Nginx Worker進程運行用戶,默認是nobody賬號。
worker_processes
這個指令名是指配置worker_processes
的數量,nginx
啓動時會有一個主進程和若干的工做進程,這個指令就是來規定工做進程的數量的,對應的是一個數值
nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.
語法: worker_processes number
缺省值: 1
error_log
這個指令是來記錄nginx
的運行出行的一些異常,能夠指定異常級別
語法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/error.log
pid
這個是用來指定運行nginx
的進程ID的;
語法: pid file
因爲不少模塊都是塊級指令集的形式的存在,本文拿出來http
模塊的部分指令來進行簡單的解析;後面的第二篇會考慮把一些實用的、經常使用的、頗有用的功能進行進一步講解。http核心模塊的指令集合、http核心模塊的指令集合
http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root 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 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; # } #} }
include
指令是主模塊的指令,能夠用在http的塊級指令集中,是防止單個配置文件過大,能夠直接引用其餘的配置文件,而例子中的 mime.types
是一個文件,裏面主要是比較全面的MIME信息,能包含文本、圖像、音頻、視頻以及其餘應用程序專用的數據和文件後綴名的映射sendfile
指令是指是否開啓linux2+的一個sendfile
的功能,sendfile詳解 server
是http模塊的重要指令,其響應http連接的關鍵,通常而言會包含listen server_name location
這三部分。localtion
指令是起到了一個路由的效果,只能在server
塊級中使用,對於各路徑和結果進行響應的設置。至於https
和一寫其餘的指令將會留到下文進行詳細的學習分析。其中可使用的指令和變量以下:
http核心模塊的指令
可在http核心模塊的塊級指令集中使用的全局變量
查看官方的文檔NGINX的文檔,能夠經過nginx
的可執行文件來啓動nginx
服務;
因此要啓動nginx,能夠這樣:
$ nginx // 通常安裝的時候都會放到系統的啓動文件夾裏面[環境變量] /usr/local/bin/nginx
在啓動以後須要使用nginx -s signal
來進行操做,其中signal
可使用如下一些指令:
若是要中止服務,能夠這樣(完成當前的全部請求後中止,和stop的區別是stop會當即中止nginx):
$ nginx -s quit
若是修改了配置文件,要從新生效,能夠這樣:
$ nginx -s reload