# 1. 基本介紹和配置文件語法
#### 1. 介紹
> nginx \[engine x\] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP proxy server, originally written by Igor Sysoev.
按照官方的定義,nginx是一個HTTP服務器,也是一個反向代理服務器。apache應該為大家所熟知,而nginx就是類似apache的提供靜態網頁的web服務器,相比于apache的多進程多線程的并發模型,而nginx是基于事件的異步IO的并發模型,性能更好,而且nginx是一個輕量級的服務器。
#### 2. 安裝
如果是ubuntu系統要安裝nginx,只需要一條命令。
```
sudo apt-get install nginx
```
如果要編譯安裝,那也簡單,也是按照三步曲來。
```
./configure
make
sudo make install
```
其中關于configure是可以按照自己的需求來配置安裝的參數的。比如:
```
./configure \
--user=nginx \
--group=nginx \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module
```
具體的編譯安裝的方法可以參考官方的這篇文章[configure](http://nginx.org/en/docs/configure.html)。
#### 3. 命令行語法
要啟動(start)、重啟(restart),停止(stop)nginx服務也很簡單。
可以這樣。
```
sudo /etc/init.d/nginx restart # or start, stop
```
或者這樣。
```
sudo service nginx restart # or start, stop
```
有時候我們改了配置文件只是要讓配置生效,這個時候不必重啟,只要重新加載配置文件即可。
```
sudo nginx -s reload
```
更多的命令可以看這篇文章[beginners\_guide](http://nginx.org/en/docs/beginners_guide.html)。
#### 4. 配置文件語法
nginx是模塊化的系統,整個系統是分成一個個模塊的。每個模塊負責不同的功能。比如http\_gzip\_static\_module就是負責壓縮的,http\_ssl\_module就是負責加密的,如果不用某個模塊的話,也可以去掉,可以讓整個nginx變得小巧,更適合自己。在上面的configure指令中帶了很多參數,就是在這里編譯之前可以加入某些模塊或去掉某些模塊的。
要用的模塊已經被編譯進nginx了,成為nginx的一部分了,那要怎么用這些模塊呢?那就得通過配置文件,這跟傳統的linux服務差不多,都是通過配置文件來改變功能。nginx的模塊是通過一個叫指令(directive)的東西來用的。整個配置文件都是由指令來控制的。nginx也有自己內置的指令,比如events, http, server, 和 location等,下面會提到的。
如果是ubuntu系統,安裝后,配置文件存放在`/etc/nginx/nginx.conf`。
把注釋的內容去掉。
```
user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
```
在這個文件中,先不管上面三行,就是由兩個block(塊)組成的。
```
events {
}
http {
}
mail {
}
```
塊和塊之間還可以嵌套的。例如http下面可以放server。
```
http {
server {
}
}
```
這個是主配置文件。有時候僅僅一個配置文件是不夠的,由其是當配置文件很大時,總不能全部邏輯塞一個文件里,所以配置文件也是需要來管理的。看最后兩行`include`,也就是說會自動包含目錄`/etc/nginx/conf.d/`的以conf結尾的文件,還有目錄`/etc/nginx/sites-enabled/`下的所有文件。
在`/etc/nginx/conf.d/`下有個配置文件叫rails.conf,它的內容大體是這樣的。
```
upstream rails365 {
# Path to Unicorn SOCK file, as defined previously
server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name www.rails365.net;
root /home/yinsigan/rails365/current/public;
keepalive_timeout 70;
...
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
...
}
server {
listen 80;
server_name rails365.net;
return 301 $scheme://www.rails365.net$request_uri;
}
```
最后整個配置文件的結構大體是這樣子的。
```
# 這里是一些配置
...
http {
# 這里是一些配置
...
# 這部分可能存在于/etc/nginx/conf.d/目錄下
upstream {
}
server {
listen 8080;
root /data/up1;
location / {
}
}
server {
listen 80;
root /data/up2;
location / {
}
}
這里是一些配置
...
}
mail {
}
```
為了篇幅,有些內容則省略了。
指令和指令之間是有層級和繼承關系的。比如http內的指令會影響到server的。
http那部分除非必要,我們不動它,假如你現在要部署一個web服務,那就在/etc/nginx/conf.d/目錄下新增一個文件就好了。
http和events還有mail是同級的。http就是跟web有關的。
server,顧名思義就是一個服務,比如你現在有一個域名,要部署一個網站,那就得創建一個server塊。
就假設你有一個域名叫foo.bar.com,要在瀏覽器輸入這個域名時就能訪問,那可能得這樣。
```
server {
listen 80;
root /home/yinsigan/foo;
server_name foo.bar.com;
location / {
}
}
```
具體的意思是這樣的。listen是監聽的端口。如果沒有特殊指定,一般網站用的都是80端口。
root是網站的源代碼靜態文件的根目錄。一般來說會在root指定的目錄下放置網站最新訪問的那個html文件,比如index.html等。
server\_name指定的是域名。
有了這些,在瀏覽器下輸入`http://foo.bar.com`就能訪問到目錄`/home/yinsigan/foo`下的index.html文件的內容。但是有時候我們得訪問`http://foo.bar.com/articles`呢?那得用location。像這樣。
```
server {
...
server_name foo.bar.com;
location /articles {
}
}
```
除了`http://foo.bar.com/articles`,還有`http://foo.bar.com/groups`,`/menus/1`等很多,是不是每個都要創建一個location啊,肯定不可能。我們有動態的方法來處理的。
下面來看一個例子。
```
server {
listen 80;
server_name example.org www.example.org;
root /data/www;
location / {
index index.html index.php;
}
location ~* \.(gif|jpg|png)$ {
expires 30d;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
當用戶訪問`http://example.org`時就會去讀取`/var/root/index.html`,如果找不到就會讀取index.php,就會轉發到`fastcgi_pass`里面的邏輯。當用戶訪問`http://example.org/about.html`就會去讀取`/var/root/about.html`文件,同樣道理,當用戶訪問`http://example.org/about.gif`就會讀取`/var/root/about.gif`文件,并會在30天后過期,也就是緩存30天。
下一篇: [nginx之反向代理(二)](http://www.rails365.net/articles/2015-10-18-nginx-zhi-fan-xiang-dai-li-er)
完結。