# 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是一個(gè)HTTP服務(wù)器,也是一個(gè)反向代理服務(wù)器。apache應(yīng)該為大家所熟知,而nginx就是類似apache的提供靜態(tài)網(wǎng)頁的web服務(wù)器,相比于apache的多進(jìn)程多線程的并發(fā)模型,而nginx是基于事件的異步IO的并發(fā)模型,性能更好,而且nginx是一個(gè)輕量級(jí)的服務(wù)器。
#### 2. 安裝
如果是ubuntu系統(tǒng)要安裝nginx,只需要一條命令。
```
sudo apt-get install nginx
```
如果要編譯安裝,那也簡(jiǎn)單,也是按照三步曲來。
```
./configure
make
sudo make install
```
其中關(guān)于configure是可以按照自己的需求來配置安裝的參數(shù)的。比如:
```
./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. 命令行語法
要啟動(dòng)(start)、重啟(restart),停止(stop)nginx服務(wù)也很簡(jiǎn)單。
可以這樣。
```
sudo /etc/init.d/nginx restart # or start, stop
```
或者這樣。
```
sudo service nginx restart # or start, stop
```
有時(shí)候我們改了配置文件只是要讓配置生效,這個(gè)時(shí)候不必重啟,只要重新加載配置文件即可。
```
sudo nginx -s reload
```
更多的命令可以看這篇文章[beginners\_guide](http://nginx.org/en/docs/beginners_guide.html)。
#### 4. 配置文件語法
nginx是模塊化的系統(tǒng),整個(gè)系統(tǒng)是分成一個(gè)個(gè)模塊的。每個(gè)模塊負(fù)責(zé)不同的功能。比如http\_gzip\_static\_module就是負(fù)責(zé)壓縮的,http\_ssl\_module就是負(fù)責(zé)加密的,如果不用某個(gè)模塊的話,也可以去掉,可以讓整個(gè)nginx變得小巧,更適合自己。在上面的configure指令中帶了很多參數(shù),就是在這里編譯之前可以加入某些模塊或去掉某些模塊的。
要用的模塊已經(jīng)被編譯進(jìn)nginx了,成為nginx的一部分了,那要怎么用這些模塊呢?那就得通過配置文件,這跟傳統(tǒng)的linux服務(wù)差不多,都是通過配置文件來改變功能。nginx的模塊是通過一個(gè)叫指令(directive)的東西來用的。整個(gè)配置文件都是由指令來控制的。nginx也有自己內(nèi)置的指令,比如events, http, server, 和 location等,下面會(huì)提到的。
如果是ubuntu系統(tǒng),安裝后,配置文件存放在`/etc/nginx/nginx.conf`。
把注釋的內(nèi)容去掉。
```
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/*;
}
```
在這個(gè)文件中,先不管上面三行,就是由兩個(gè)block(塊)組成的。
```
events {
}
http {
}
mail {
}
```
塊和塊之間還可以嵌套的。例如http下面可以放server。
```
http {
server {
}
}
```
這個(gè)是主配置文件。有時(shí)候僅僅一個(gè)配置文件是不夠的,由其是當(dāng)配置文件很大時(shí),總不能全部邏輯塞一個(gè)文件里,所以配置文件也是需要來管理的??醋詈髢尚衊include`,也就是說會(huì)自動(dòng)包含目錄`/etc/nginx/conf.d/`的以conf結(jié)尾的文件,還有目錄`/etc/nginx/sites-enabled/`下的所有文件。
在`/etc/nginx/conf.d/`下有個(gè)配置文件叫rails.conf,它的內(nèi)容大體是這樣的。
```
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;
}
```
最后整個(gè)配置文件的結(jié)構(gòu)大體是這樣子的。
```
# 這里是一些配置
...
http {
# 這里是一些配置
...
# 這部分可能存在于/etc/nginx/conf.d/目錄下
upstream {
}
server {
listen 8080;
root /data/up1;
location / {
}
}
server {
listen 80;
root /data/up2;
location / {
}
}
這里是一些配置
...
}
mail {
}
```
為了篇幅,有些內(nèi)容則省略了。
指令和指令之間是有層級(jí)和繼承關(guān)系的。比如http內(nèi)的指令會(huì)影響到server的。
http那部分除非必要,我們不動(dòng)它,假如你現(xiàn)在要部署一個(gè)web服務(wù),那就在/etc/nginx/conf.d/目錄下新增一個(gè)文件就好了。
http和events還有mail是同級(jí)的。http就是跟web有關(guān)的。
server,顧名思義就是一個(gè)服務(wù),比如你現(xiàn)在有一個(gè)域名,要部署一個(gè)網(wǎng)站,那就得創(chuàng)建一個(gè)server塊。
就假設(shè)你有一個(gè)域名叫foo.bar.com,要在瀏覽器輸入這個(gè)域名時(shí)就能訪問,那可能得這樣。
```
server {
listen 80;
root /home/yinsigan/foo;
server_name foo.bar.com;
location / {
}
}
```
具體的意思是這樣的。listen是監(jiān)聽的端口。如果沒有特殊指定,一般網(wǎng)站用的都是80端口。
root是網(wǎng)站的源代碼靜態(tài)文件的根目錄。一般來說會(huì)在root指定的目錄下放置網(wǎng)站最新訪問的那個(gè)html文件,比如index.html等。
server\_name指定的是域名。
有了這些,在瀏覽器下輸入`http://foo.bar.com`就能訪問到目錄`/home/yinsigan/foo`下的index.html文件的內(nèi)容。但是有時(shí)候我們得訪問`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`等很多,是不是每個(gè)都要?jiǎng)?chuàng)建一個(gè)location啊,肯定不可能。我們有動(dòng)態(tài)的方法來處理的。
下面來看一個(gè)例子。
```
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;
}
}
```
當(dāng)用戶訪問`http://example.org`時(shí)就會(huì)去讀取`/var/root/index.html`,如果找不到就會(huì)讀取index.php,就會(huì)轉(zhuǎn)發(fā)到`fastcgi_pass`里面的邏輯。當(dāng)用戶訪問`http://example.org/about.html`就會(huì)去讀取`/var/root/about.html`文件,同樣道理,當(dāng)用戶訪問`http://example.org/about.gif`就會(huì)讀取`/var/root/about.gif`文件,并會(huì)在30天后過期,也就是緩存30天。
下一篇: [nginx之反向代理(二)](http://www.rails365.net/articles/2015-10-18-nginx-zhi-fan-xiang-dai-li-er)
完結(jié)。
- 介紹
- 安裝
- 1. 基本介紹和配置文件語法
- 2. 反向代理
- 3. gzip 壓縮提升網(wǎng)站性能
- 4. 在線升級(jí)
- 5. 監(jiān)控工具 ngxtop
- 6. 編譯第三方模塊
- 7. auth_basic 模塊使用
- 8. 日志分析工具
- 9. 用 nginx 搭建谷歌鏡像網(wǎng)站
- 10. 自制啟動(dòng)腳本
- 11. 日志切割
- 12. 作為負(fù)載均衡器
- 13. 開啟 debug 模式
- 14. gzip static 模塊探索
- 15. 安裝最新 nginx 的另類方法
- 16. 使用 acme.sh 安裝 Let’ s Encrypt 提供的免費(fèi) SSL 證書
- 17. 給 GitLab 應(yīng)用加上 https
