源码安装nginx并详解配置

背景

Nginx是一款轻量级的网页服务器、反向代理服务器.相较于Apache、lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供反向代理服务.

安装依赖

yum -y install gcc gcc-c++ autoconf automake make

nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库

安装PCRE

1
2
3
4
5
6
7
cd ~/source-module
wget http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz

tar -xzvf pcre-8.41.tar.gz
cd pcre-8.41
./configure --prefix=/usr/local
make
make install

安装zlib

1
2
3
4
5
6
7
cd ~/source-module
wget http://zlib.net/fossils/zlib-1.2.8.tar.gz
tar -xzvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local
make
make install

安装ssl

1
2
3
4
5
6
cd /usr/local
sudo wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
sudo tar -xzvf openssl-1.0.1j.tar.gz
./Configure darwin64-x86_64-cc
make
make install

安装nginx

下载源码并安装

1
2
3
4
5
6
wget https://nginx.org/download/nginx-1.12.1.tar.gz
tar -xzvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --prefix=/Users/ac/devTools/nginx-1.12.1 --with-pcre=/Users/ac/source-module/pcre-8.41 --with-zlib=/Users/ac/source-module/zlib-1.2.8
make
make install

在已运行的nginx上添加扩展模块

注: 该部分文章, 与上面文章不在一台机器上操作, 注意路径即可.

下面以添加fastdfs-nginx-module模块为例进行操作.

查看当前已安装模块

1
2
3
4
[root@Node nginx-1.12.1]# nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
configure arguments: --prefix=/root/devTools/nginx-1.12.1 --with-pcre=/root/workspaces/local/pcre-8.41 --with-zlib=/root/workspaces/local/zlib-1.2.8
1
2
3
4
5
cd ~/workspaces/github
git clone git@github:happyfish100/fastdfs-nginx-module.git
cd /workspaces/local/nginx-1.12.1
./configure --prefix=/root/devTools/nginx-1.12.1 --with-pcre=/root/workspaces/local/pcre-8.41 --with-zlib=/root/workspaces/local/zlib-1.2.8 --add-module=/root/workspaces/github/fastdfs-nginx-module/src/
make

注: 切记不要执行make install指令, 否则将覆盖之前的配置文件

接下来切换到nginx安装目录, 备份可执行的二进制文件, 复制./objs目录下的对应文件到安装目录即可, 如下:

1
2
3
cd /root/devTools/nginx-1.12.1/
mv nginx nginx.bak
cp /root/workspaces/local/nginx-1.12.1/objs/nginx ./

查看

1
2
3
4
[root@Node nginx-1.12.1]# nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
configure arguments: --prefix=/root/devTools/nginx-1.12.1 --with-pcre=/root/workspaces/local/pcre-8.41 --with-zlib=/root/workspaces/local/zlib-1.2.8 --add-module=/root/workspaces/github/fastdfs-nginx-module/src/

指令

将安装后的目录加入PATH中, 方便调用指令

编辑.bash_profile文件

1
2
export NGINX_HOME=/Users/ac/devTools/nginx-1.12.1
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$MYSQL_HOME/bin:$SCALA_HOME/bin:$SBT_HOME/bin:$NGINX_HOME/sbin:$NODE_HOME/bin:$PATH

更新profile文件, source ~/.bash_profile

  • 查看版本: nginx -v
  • 检查配置文件是否正常: nginx -t
  • 启动: nginx
  • 重启: nginx -s reload
  • 停止: nginx -s stop
  • 强制停止: pkill nginx

注: 若提示Permission denied, 请在命令前添加sudo或切换到root用户(使用su命令)

配置

配置文件位置

1
/Users/ac/devTools/nginx-1.12.1/conf/nginx.conf

Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值.

server

1
2
3
4
5
6
server {
listen 80;
server_name ts.gyenno.com;
root html;
index index.html index.htm;
}

建议有多个server时, 将各server独立放入一个*.conf文件中(例如: gyenno.conf), 并在nginx.conf文件内容末尾加入如下语句include conf.d/*.conf;

location

每个 url 请求都会对应的一个服务,nginx 进行处理转发或者是本地的一个文件路径,或者是其他服务器的一个服务路径.而这个路径的匹配是通过 location 来进行的.我们可以将 server 当做对应一个域名进行的配置,而 location 是在一个域名下对更精细的路径进行配置.

以上面的例子,可以将root和index指令放到一个location中,那么只有在匹配到这个location时才会访问root后的内容:

1
2
3
4
location / {
root /data/www/gyenno;
index index.html index.htm;
}

location 匹配规则

1
2
3
4
~      波浪线表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
^~ ^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= 进行普通字符精确匹配

匹配示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
location  = / {
# 只匹配"/".
[ configuration A ]

}
location / {
# 匹配任何请求,因为所有请求都是以"/"开始
# 但是更长字符匹配或者正则表达式匹配会优先匹配
[ configuration B ]

}
location ^~ /images/ {
# 匹配任何以 /images/ 开始的请求,并停止匹配其它location
[ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg结尾的请求.
# 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
[ configuration D ]
}

请求:
/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D

静态文件映射

rootalias两个指令指定文件目录

autoindex on: 列出指定目录下的文件

root

1
2
3
location /b/ {
root /a/
}

文件所在目录: root + location

例如: http://localhost/b/index.html, index.html所在目录为/a/b/

注: root后的/可以省略

alias

1
2
3
location /b/ {
alias /a/
}

文件所在目录: alias

例如: http://localhost/b/index.html, index.html所在目录为/a/

注: root后的/不可省略

转发

1
2
3
location / {
proxy_pass 127.0.0.1:8080
}

所有访问ts.gyenno.com的请求都转发到本地机器的8080端口.

负载均衡

1
2
3
4
5
6
7
8
9
10
upstream myserver {
ip_hash;
server 10.16.1.1:8081;
server 10.16.1.2:8082;
server 10.16.1.3;
server 10.16.1.4;
}
location / {
proxy_pass http://myserver;
}

我们在 upstream 中指定了一组机器,并将这个组命名为 myserver,这样在 proxypass 中只要将请求转移到 myserver 这个 upstream 中我们就实现了在四台机器的反向代理加负载均衡.其中的 ip_hash 指明了我们均衡的方式是按照用户的 ip 地址进行分配.另外还有轮询、指定权重轮询、fair、url_hash几种调度算法.

总结

以上是最简单的通过 nginx 实现静态文件转发、反向代理和负载均衡的配置.在 nginx 中所有的功能都是通过模块来实现的,比如当我们配置 upstream 时是用 upstream 模块,而 server 和 location 是在 http core 模块,其他的还有流控的 limt 模块,邮件的 mail 模块,https 的 ssl 模块.他们的配置都是类似的可以再 nginx的模块文档中找到详细的配置说明.

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.