公司剛使用nginx,預先學習下。鑒於機器沒有Linux環境,在Windows熟悉下。
下載
目前(2015-07-11),nginx的穩定版本是1.8.0,在官網下載先,windows版的nginx1.8.0
這是一個zip文件,解壓后即可使用
Windows下的安裝與啟動
綠色文件,無須安裝,直接即可啟動。
據我所知,3種啟動途徑,其實都類似:
一、雙擊nginx.exe圖標,可見黑窗口一閃而過,啟動完畢。
二、命令行到nginx目錄,輸入nginx啟動。(注,此方式命令行窗口無任何提示,且被鎖定)
三、命令行到nginx目錄,輸入start nginx啟動,此方式不鎖定
啟動后,默認情況下(無修改配置),可見到有兩個nginx的進程,1個是master process,1個是worker processes。
如果你在配置中配置2個worker_processes,進程中就可以看到1個是master process,2個是worker processes。
測試
默認nginx部署了些靜態內容,我們可通過它測試nginx是否在工作。
默認的配置文件(NGINX_HOME/conf/nginx.conf)如下:

#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;
# }
#}
}
通過觀察配置文件的非注釋項(參考Nginx配置文件nginx.conf中文詳解),大概可知:
1、啟動了1個worker processes
2、worker_connections,最大並發數為1024
3、include mime.types,引入mime.types文件所聲明的文件擴展名與文件類型映射
4、application/octet-stream,默認使用application/octet-stream
5、sendfile,開啟高效文件傳輸模式
6、監聽本機“localhost”的80端口
7、映射目錄為“當前目錄的html目錄”
8、出現500、502、503、504錯誤,則映射到50x.html
瀏覽地址http://localhost,即可訪問其默認頁面,即映射到NGINX_HOME/html/index.html
其他靜態內容,如html、圖片,可自行添加測試。
日志
日志默認位於NGINX_HOME/logs/,可見:
1、access.log,訪問日志
2、error.log,異常日志
3、nginx.pid,進程(僅在啟動nginx后才有此日志)
CentOS下Nginx的安裝與啟動
tar -zxvf nginx-1.8.0.tar.gz cd nginx-1.8.0 ./configure make make install /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
安裝完畢,查看進程及相關日志:
ps -ef | grep nginx less /usr/local/nginx/logs/error.log less /usr/local/nginx/logs/nginx.pid less /usr/local/nginx/logs/access.log
較詳細的Nginx在Linux的博文見《【Nginx】Nginx在Linux下的入門介紹》。