Nginx
安装 Nginx
~$ sudo apt-get install nginx
The following extra packages will be installed:
libgd2-noxpm libjpeg-turbo8 libjpeg8 nginx-common nginx-full
Suggested packages:
libgd-tools
The following NEW packages will be installed:
libgd2-noxpm libjpeg-turbo8 libjpeg8 nginx nginx-common nginx-full
查看 nginx 都安装了哪些文件及安装目录:
~$ dpkg -l | grep nginx
ii nginx 1.1.19-1ubuntu0.7 small, but very powerful and efficient web server and mail proxy
ii nginx-common 1.1.19-1ubuntu0.7 small, but very powerful and efficient web server (common files)
ii nginx-full 1.1.19-1ubuntu0.7 nginx web server with full set of core modules
~$ dpkg -L nginx
~$ dpkg -L nginx-common
~$ dpkg -L nginx-full
重点留意如下文件:
/lib/systemd/system/nginx.service
/usr/share/nginx
/usr/share/nginx/www
/usr/share/nginx/www/index.html
/usr/share/nginx/www/50x.html
/etc/init.d/nginx
/etc/ufw/applications.d/nginx
/etc/default/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/nginx.conf
/etc/logrotate.d/nginx
/var/lib/nginx
/var/log/nginx
/usr/sbin/nginx
配置:/etc/nginx/nginx.conf
日志:/var/log/nginx
默认网页:/usr/share/nginx/www/index.html
Nginx 基本使用
Nginx 的使用非常简洁,通过 nginx -h 或 man nginx 即可查看。常用命令:
nginx -t: test configuration and exit
nginx -s stop: fast shutdown
nginx -s quit: graceful shutdown(一般关闭 nginx 用此选项)
nginx -s reload: reloading the configuration file
nginx -s reopen: reopening the log files
nginx -p prefix : set prefix path(prefix 并非是 working directory,仅仅是默认配置路径(prefix/conf/nginx.conf)、默认日志路径(prefix/logs/error.log, prefix/logs/access.log)、默认d 文件目录(prefix/logs/nginx.pid)等的前缀,因此,配置中的相对路径(logs/nginx.pid, ogs/access.log, etc.)不是相对 working directory, 而是相对 prefix,它自身的默认值在 configure 时指定)(https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/)
Note: -s 是发送信号,那么怎么知道往哪个进程发送信号呢?关键在于 pid 文件,pid 文件存有进程的 pid, 执行 -s 操作就是向这个 pid 发送信号,但是 pid 文件的配置跟 prefix 和配置文件(pid 指令)都相关,因此执行 -s 操作的命令必须跟启动命令匹配,即:
如果启动命令是:
/opt/nginx/sbin/nginx -p /opt/nginx/ -c /opt/nginx/conf/nginx.conf
那么,-s 命令就是:
/opt/nginx/sbin/nginx -p /opt/nginx/ -c /opt/nginx/conf/nginx.conf -s quit
如果不匹配,则很可能找不到 pid 文件或找到错误的 pid 文件,可能杀死其他进程,产生意想不到的后果。笔者就是在一台机器上部署了两套 nginx, 一套生产用,一套测试用,生产采用默认配置,结果不小心结束测试用的 nginx 时没有指定这些参数,意外把生产环境的 nginx 结束了。
~$ sudo nginx
~$ ps aux | grep nginx
root 2851 0.0 0.0 62864 1212 ? Ss 22:45 0:00 nginx: master process nginx
www-data 2852 0.0 0.0 63220 1900 ? S 22:45 0:00 nginx: worker process
www-data 2853 0.0 0.0 63220 1900 ? S 22:45 0:00 nginx: worker process
www-data 2854 0.0 0.0 63220 1640 ? S 22:45 0:00 nginx: worker process
www-data 2855 0.0 0.0 63220 1896 ? S 22:45 0:00 nginx: worker process
hwx 2857 0.0 0.0 9392 940 pts/0 S+ 22:45 0:00 grep --color=auto nginx
~$ sudo nginx -s quit
~$ ps aux | grep nginx
hwx 2862 0.0 0.0 9392 940 pts/0 S+ 22:45 0:00 grep --color=auto nginx
启动 nginx 之后通过浏览器访问,将显示 /usr/share/nginx/www/index.html 中的内容。
References
http://nginx.org/en/docs/beginners_guide.html
http://wiki.nginx.org/CommandLine
Nginx 配置
Nginx 配置的基本原则
1. 每一个配置项都是一个指令(directive),http://nginx.org/en/docs/dirindex.html
2. nginx 由多个模块组成,http://nginx.org/en/docs/
3. 每个指令属于一个上下文环境(context),如 http 指令属于最顶层的 context(main)(http://nginx.org/en/docs/http/ngx_http_core_module.html#http), 而 sever 属于 http 这个 context(http://nginx.org/en/docs/http/ngx_http_core_module.html#server); 每个指令属于特性的模块,如 http 指令属于 http core 模块(http://nginx.org/en/docs/http/ngx_http_core_module.html)
例子——配置多个虚拟主机
配置虚拟主机可以在 /etc/nginx/nginx.conf 的 http block 中直接添加 server block, 但是这样做不够优雅, /etc/nginx/nginx.conf http block 有这么一段:
http {
......
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
可以用文件包含的方式,一个文件配置一个主机,事实上,我们在安装完 nginx 之后访问 http://localhost/ 出现的页面正是 /etc/nginx/sites-enabled/default 呈现的。下面我们增加两个虚拟主机,一个是 example.com(www.example.com), 一个是 example-php.com(www.example-php.com). 配置文件分别为 /etc/nginx/conf.d/example.com.conf 和 /etc/nginx/conf.d/example-php.com.conf.
配置变更之后注意先测试配置文件是否正确:
~$ sudo nginx -t
nginx: the configuration file /etc/nginx/ng