世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页Nginx
随风 · 练气

Nginx 学习笔记系列文章之 Nginx 之基本介绍和配置文件语法 (一)

随风发布于5715 次阅读

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

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

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 之反向代理 (二)

完结。

本站文章均为原创内容,如需转载请注明出处,谢谢。

0 条回复
暂无回复~~
相关小书
nginx教程

nginx教程

最全面,最深入的nginx入门到精通的教程

发表于

喜欢
统计信息
    学员: 29204
    视频数量: 1985
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top