3.1 nginx 常用的命令
使用nginx命令必須進(jìn)入nginx目錄下。
- 啟動(dòng)命令
在/usr/local/nginx/sbin 目錄下執(zhí)行
./nginx
- 關(guān)閉命令
在/usr/local/nginx/sbin目錄下執(zhí)行
./nginx -s stop
- 重新加載命令(在nginx開啟情況下 重新加載文件)
在/usr/local/nginx/sbin 目錄下執(zhí)行
./nginx -s reload
- 查看版本號(hào)
./nginx -v
3.2 nginx.conf 配置文件
nginx 安裝目錄下,其默認(rèn)的配置文件都放在這個(gè)目錄的 conf 目錄下,而主配置文件 nginx.conf 也在其中,后續(xù)對(duì) nginx 的使用基本上都是對(duì)此配置文件進(jìn)行相應(yīng)的修改。
配置文件中有很多#, 開頭的表示注釋內(nèi)容,我們?nèi)サ羲幸?# 開頭的段落,精簡之后的內(nèi)容如下:



#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 80;
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;
# }
#}
}
根據(jù)上述文件,我們可以很明顯的將 nginx.conf 配置文件分為三部分:
第一部分:全局塊
從配置文件開始到 events 塊之間的內(nèi)容,主要會(huì)設(shè)置一些影響 nginx 服務(wù)器整體運(yùn)行的配置指令,主要包括配置運(yùn)行 Nginx 服務(wù)器的用戶(組)、允許生成的 worker process 數(shù),進(jìn)程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。
比如上面第一行配置的:

這是 Nginx 服務(wù)器并發(fā)處理服務(wù)的關(guān)鍵配置,worker_processes 值越大,可以支持的并發(fā)處理量也越多,但是會(huì)受到硬件、軟件等設(shè)備的制約。
第二部分:events 塊
比如上面的配置:

events 塊涉及的指令主要影響 Nginx 服務(wù)器與用戶的網(wǎng)絡(luò)連接,常用的設(shè)置包括是否開啟對(duì)多 work process 下的網(wǎng)絡(luò)連接進(jìn)行序列化,是否允許同時(shí)接收多個(gè)網(wǎng)絡(luò)連接,選取哪種事件驅(qū)動(dòng)模型來處理連接請(qǐng)求,每個(gè) word process 可以同時(shí)支持的最大連接數(shù)等。
上述例子就表示每個(gè) work process 支持的最大連接數(shù)為 1024. 這部分的配置對(duì) Nginx 的性能影響較大,在實(shí)際中應(yīng)該靈活配置
第三部分:http 塊

這算是 Nginx 服務(wù)器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數(shù)功能和第三方模塊的配置都在這里。
需要注意的是:http 塊也可以包括 http 全局塊、server 塊。
①、http 全局塊
http 全局塊配置的指令包括文件引入、MIME-TYPE 定義、日志自定義、連接超時(shí)時(shí)間、單鏈接請(qǐng)求數(shù)上限等。
②、server 塊
這塊和虛擬主機(jī)有密切關(guān)系,虛擬主機(jī)從用戶角度看,和一個(gè)的硬件主機(jī)是完全一樣的,該技術(shù)的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務(wù)器硬件成本。
每個(gè) http 塊可以包括多個(gè) server 塊,而每個(gè) server 塊就相當(dāng)于一個(gè)虛擬主機(jī)。
而每個(gè) server 塊也分為全局 server 塊,以及可以同時(shí)包含多個(gè) locaton 塊
1、全局 server 塊
最常見的配置是本虛擬機(jī)主機(jī)的監(jiān)聽配置和本虛擬主機(jī)的名稱或 IP 配置。
2、location 塊
一個(gè) server 塊可以配置多個(gè) location 塊。
這塊的主要作用是基于 Nginx 服務(wù)器接收到的請(qǐng)求字符串(例如 server_name/uri-string),對(duì)虛擬主機(jī)名稱(也可以是 IP 別名)之外的字符串(例如 前面的 /uri-string)進(jìn)行匹配,對(duì)特定的請(qǐng)求進(jìn)行處理。地址定向、數(shù)據(jù)緩存和應(yīng)答控制等功能,還有許多第三方模塊的配置也在這里進(jìn)行。
感興趣的話點(diǎn)點(diǎn)關(guān)注,我們一起成長進(jìn)步。