![](https://source.unsplash.com/random/1024x200) >[warning] 定义 > 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是一个轻量级的服务器。 >[warning] 安装 如果是ubuntu系统要安装nginx,只需要一条命令。 ``` bash sudo apt-get install nginx ``` 如果要编译安装,那也简单,也是按照三步曲来。 ``` bash ./configure make sudo make install ``` 其中关于configure是可以按照自己的需求来配置安装的参数的。比如: ``` bash ./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)。 >[warning] 命令行语法 要启动(start)、重启(restart),停止(stop)nginx服务也很简单。 可以这样。 ``` bash sudo /etc/init.d/nginx restart # or start, stop ``` 或者这样。 ``` bash sudo service nginx restart # or start, stop ``` 有时候我们改了配置文件只是要让配置生效,这个时候不必重启,只要重新加载配置文件即可。 ``` bash sudo nginx -s reload ``` 更多的命令可以看这篇文章[beginners_guide](http://nginx.org/en/docs/beginners_guide.html)。 >[success] #### 配置文件语法 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`。 把注释的内容去掉。 ``` 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(块)组成的。 ``` conf events { } http { } mail { } ``` 块和块之间还可以嵌套的。例如http下面可以放server。 ``` conf http { server { } } ``` 这个是主配置文件。有时候仅仅一个配置文件是不够的,由其是当配置文件很大时,总不能全部逻辑塞一个文件里,所以配置文件也是需要来管理的。看最后两行`include`,也就是说会自动包含目录`/etc/nginx/conf.d/`的以conf结尾的文件,还有目录`/etc/nginx/sites-enabled/`下的所有文件。 在`/etc/nginx/conf.d/`下有个配置文件叫rails.conf,它的内容大体是这样的。 ``` 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; } ``` 最后整个配置文件的结构大体是这样子的。 ``` conf # 这里是一些配置 ... 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,要在浏览器输入这个域名时就能访问,那可能得这样。 ``` conf 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。像这样。 ``` conf server { ... server_name foo.bar.com; location /articles { } } ``` 除了`http://foo.bar.com/articles`,还有`http://foo.bar.com/groups`,`/menus/1`等很多,是不是每个都要创建一个location啊,肯定不可能。我们有动态的方法来处理的。 下面来看一个例子。 ``` conf 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天。