目录标题
序言:nginx可以用作web服务器,也可以用作反向代理服务器,本文介绍nginx的下载,安装,配置及启停等基本命令
1、nginx下载
可以直接在nginx的官网地址:下载稳定版本(stableversion)
或则直接使用centos的wget命令
# cd /usr/local
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、nginx安装
先执行以下命令,安装nginx依赖库,倘若缺乏依赖库,可能会安装失败,具体可以参考文章旁边的错误提示信息。
yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
解压安装包
tar -zxvf nginx-1.16.1.tar.gz
nginx被解压到了/usr/local/nginx-1.16.1目录下(不要把压缩包解压到/usr/local/nginx目录下,或则将解压后的目录重命名为nginx,由于nginx会默认安装到/usr/local/nginx目录下),切换到nginx-1.16.1目录
cd /usr/local/nginx-1.16.1/
./configure
该操作会检查当前系统环境,以确保能成功安装nginx,执行该操作后可能会出现以下几种提示:
checking for OS
+ Linux 3.10.0-123.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
如果出现以上错误提示信息,执行yum install gcc-c++安装gcc,
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
如果出现上面提示,表示缺少PCRE库
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
若果出现以上提示,表示缺乏zlib库。假如没有出现./configure:error提示linux操作系统下载,表示当前环境可以安装nginx,执行make和makeinstall编译nginx
make
make install
没有出错的话,表示nginx早已成功安装完成,默认安装位置为/usr/local/nginx。倘若出现cp:‘conf/koi-win’and‘/usr/local/nginx/conf/koi-win’arethesamefile,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,makeinstall.
3、nginx启动
/usr/local/nginx/sbin/nginx
检测是否启动成功:打开浏览器访问此机器的IP,假如浏览器出现Welcometonginx!则表示Nginx早已安装并运行成功。
其他命令如下:
#重启:
/usr/local/nginx/sbin/nginx -s reload
#停止:
/usr/local/nginx/sbin/nginx -s stop
#测试配置文件是否正常:
/usr/local/nginx/sbin/nginx -t
#强制关闭:
pkill nginx
4、nginx配置
以上安装方式nginx的配置文件坐落
/usr/local/nginx/conf/nginx.conf
Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的承继关系是从外到内,也就是说外层块会手动获取内层块的值作为缺省值。
4.1Server
接收恳求的服务器须要将不同的恳求按规则转发到不同的前端服务器上,在nginx中我们可以通过建立虚拟主机(server)的概念来将这种不同的服务配置隔离。
server{
listen80;
server_namelocalhost;
roothtml;
indexindex.htmlindex.htm;
比如我们笔戈玩下的两个子项目passport和wan就可以通过在nginx的配置文件中配置两个server,servername分别为和。这样的话不同的url恳求都会对应到nginx相应的设置,转发到不同的前端服务器上。
这儿的listen指窃听端口centos nginx 安装,server_name拿来指定IP或域名,多个域名对应统一规则可以空格分开,index用于设定访问的默认首页地址,root指令用于指定虚拟主机的网页跟目录,这个地方可以是相对地址也可以是绝对地址。
一般情况下我们可以在nginx.conf中配置多个server,对不同的恳求进行设置。如同这样:
server{
listen80;
server_namehost1;
roothtml;
indexindex.htmlindex.htm;
server{
listen80;
server_namehost2;
root/data/www/html;
indexindex.htmlindex.htm;
并且当server超过2个时,建议将不同对虚拟主机的配置置于另一个文件中,之后通过在主配置文件nginx.conf加上include指令包含进来。更易于管理。
includevhosts/*.conf;
就可以把vhosts的文件都包含进去啦。
4.2Localtion
每位url恳求还会对应的一个服务,nginx进行处理转发或则是本地的一个文件路径,或则是其他服务器的一个服务路径。而这个路径的匹配是通过location来进行的。我们可以将server当作对应一个域名进行的配置,而location是在一个域名下对更精细的路径进行配置。
以里面的事例linux使用教程,可以将root和index指令放在一个location中,这么只有在匹配到这个location时才能访问root后的内容:
location / {
root /data/www/host2;
index index.html index.htm;
}
location匹配规则
~波浪线表示执行一个正则匹配centos nginx 安装,分辨大小写
~*表示执行一个正则匹配,不分辨大小写
^~^~表示普通字符匹配,假如该选项匹配,只匹配该选项,不匹配别的选项,通常拿来匹配目录
=进行普通字符精确匹配
匹配事例:
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 ]
}
恳求:
/->符合configurationA
/documents/document.html->符合configurationB
/images/1.gif->符合configurationC
/documents/1.jpg->符合configurationD
4.3静态文件映射
访问文件的配置主要有root和aliasp’s两个指令。这两个指令的区别容易弄错:
alias
alias后跟的指定目录是确切的,而且末尾必须加/。
location /c/ {
alias /a/;
}
假如访问站点访问的就是/a/目录下的站点信息。
root
root后跟的指定目录是上级目录,但是该上级目录下要富含和location后指定名称的同名目录才行。
location /c/ {
root /a/;
}
这时访问站点访问的就是/a/c目录下的站点信息。
假如你须要将这个目录展开,在这个location的末尾加上「autoindexon;」就可以了
转发
配置上去很简单例如我要将所有的恳求到转移到真正提供服务的一台机器的8001端口,只要这样:
location / {
proxy_pass 172.16.1.1:8001;
}
这样访问host时,就都被转发到172.16.1.1的8001端口去了。
负载均衡
upstream myserver; {
ip_hash;
server 172.16.1.1:8001;
server 172.16.1.2:8002;
server 172.16.1.3;
server 172.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是在httpcore模块,其他的还有流控的limt模块,短信的mail模块,https的ssl模块。她们的配置都是类似的,可以查看
关于nginx配置的详尽参考:。
一个配置nginx恳求转发的config配置文件示例(删掉原nginx.conf的注释部份):
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 9000;
server_name localhost;
location /{
root html;
index index.html index.htm;
}
location ^~ /api/ {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
proxy_pass http://127.0.0.1:9003/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
文章主要参考以下文章并结合实践以及整理:
Centos下Nginx安装与配置:
CentOS7安装Nginx并配置手动启动:
文章评论