CentOS7安装Nginx及配置

 

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

安装


  1.  
    在Centos下,yum源不提供nginx的安装,能够经过切换yum源的方法获取安装。也能够经过直接下载安装包的方法,**如下命令均需root权限执行**:
  2.  
    首先安装必要的库(nginx 中gzip模块须要 zlib 库,rewrite模块须要 pcre 库,ssl 功能须要openssl库)。选定**/usr/ local**为安装目录,如下具体版本号根据实际改变。

1.安装gcc gcc-c++(如新环境,未安装请先安装)javascript

  1.  
    $ yum install -y gcc gcc-c++
  2.  
     

2.安装PCRE库php

  1.  
    $ cd /usr/local/
  2.  
    $ wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
  3.  
    $ tar -zxvf pcre-8.36.tar.gz
  4.  
    $ cd pcre-8.36
  5.  
    $ ./configure
  6.  
    $ make && make install
  7.  
     
  8.  
    如报错:configure: error: You need a C++ compiler for C++ support
  9.  
    解决:yum install -y gcc gcc-c++
  10.  
     

3.安装SSL库css

  1.  
    $ cd /usr/local/
  2.  
    $ wget http://www.openssl.org/ source/openssl-1.0.1j.tar.gz
  3.  
    $ tar -zxvf openssl-1.0.1j.tar.gz
  4.  
    $ cd openssl-1.0.1j
  5.  
    $ ./config
  6.  
    $ make && make install
  7.  
     
  8.  
    4.安装zlib库存

$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ ./configure
$ make && make installhtml

 

4.安装nginxjava

  1.  
    $ cd /usr/local/
  2.  
    $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
  3.  
    $ tar -zxvf nginx-1.8.0.tar.gz
  4.  
    $ cd nginx-1.8.0
  5.  
    $ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
  6.  
    (注: --with-http_ssl_module:这个不加后面在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常)
  7.  
    $ make && make install
  8.  

    时提示如下错误:linux

    ./configure: error: SSL modules require the OpenSSL library.nginx

    支持此命令:c++

    yum -y install openssl openssl-devel正则表达式

  9.  
    报错:./configure: error: the HTTP gzip module requires the zlib library
  10.  
     

在–prefix后面接如下命令:算法

  1.  
    --with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源码路径。--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。
  2.  
     

点击此处下载安装脚本

5.启动

$ /usr/local/nginx/sbin/nginx 

检查是否启动成功:

打开浏览器访问此机器的 IP,若是浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

部分命令以下:
重启:

$ /usr/local/nginx/sbin/nginx –s reload 

中止:

$ /usr/local/nginx/sbin/nginx –s stop 

测试配置文件是否正常:

$ /usr/local/nginx/sbin/nginx –t 

强制关闭:

  1.  
    $ pkill nginx
  2.  
     

配置


以上安装方法nginx的配置文件位于

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

Server

接收请求的服务器须要将不一样的请求按规则转发到不一样的后端服务器上,在 nginx 中咱们能够经过构建虚拟主机(server)的概念来将这些不一样的服务配置隔离。

server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
}

例如咱们笔戈玩下的两个子项目 passport 和 wan 就能够经过在 nginx 的配置文件中配置两个 server,servername 分别为 passport.bigertech.com 和 wan.bigertech.com。这样的话不一样的 url 请求就会对应到 nginx 相应的设置,转发到不一样的后端服务器上。
这里的 listen 指监听端口,server_name 用来指定IP或域名,多个域名对应统一规则能够空格分开,index 用于设定访问的默认首页地址,root 指令用于指定虚拟主机的网页跟目录,这个地方能够是相对地址也能够是绝对地址。
一般状况下咱们能够在 nginx.conf 中配置多个server,对不一样的请求进行设置。就像这样:

server {
listen 80;
server_name host1;
root html;
index index.html
index.htm;
}
server {
listen 80;
server_name host2;
root /data/www/html;
index index.html index.htm;
}

可是当 server 超过2个时,建议将不一样对虚拟主机的配置放在另外一个文件中,而后经过在主配置文件 nginx.conf 加上 include 指令包含进来。更便于管理。

include vhosts/*.conf;

就能够把vhosts的文件都包含进去啦。

Localtion
每一个 url 请求都会对应的一个服务,nginx 进行处理转发或者是本地的一个文件路径,或者是其余服务器的一个服务路径。而这个路径的匹配是经过 location 来进行的。咱们能够将 server 当作对应一个域名进行的配置,而 location 是在一个域名下对更精细的路径进行配置。

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

location / {

  1.  
    root /data/www/host2;
  2.  
    index index.html index.htm;

}

location 匹配规则

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

匹配例子:

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

静态文件映射
访问文件的配置主要有 root 和 aliasp’s 两个指令。这两个指令的区别容易弄混:
alias
alias后跟的指定目录是准确的,而且末尾必须加 /。

location /c/ {
alias /a/;
}

root
root后跟的指定目录是上级目录,而且该上级目录下要含有和location后指定名称的同名目录才行。

location /c/ {
root /a/;
}

若是你须要将这个目录展开,在这个location的末尾加上「autoindex on; 」就能够了

转发
配置起来很简单好比我要将全部的请求到转移到真正提供服务的一台机器的 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 是在 http core 模块,其余的还有流控的 limt 模块,邮件的 mail 模块,https 的 ssl 模块。他们的配置都是相似的能够再 nginx 的模块文档中找到详细的配置说明。

 

*********************************************************************************

 

文章转自:http://www.javashuo.com/article/p-encyalxj-cq.html

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,固然,也能够安装 Windows 版本,本篇则使用 CentOS 7 做为安装环境。

 

一. gcc 安装
安装 nginx 须要先将官网下载的源码进行编译,编译依赖 gcc 环境,若是没有 gcc 环境,则须要安装:

yum install gcc-c++

二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,因此须要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也须要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装
zlib 库提供了不少种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,因此须要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不只支持 http 协议,还支持 https(即在ssl协议上传输http),因此须要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

官网下载

1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

 

2.使用wget命令下载(推荐)。确保系统已经安装了wget,若是没有安装,执行 yum install wget 安装。

wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

 

我下载的是1.12.0版本,这个是目前的稳定版。

解压

依然是直接命令:

tar -zxvf nginx-1.12.0.tar.gz cd nginx-1.12.0

配置

其实在 nginx-1.12.0 版本中你就不须要去配置相关东西,默认就能够了。固然,若是你要本身配置目录也是能够的。
1.使用默认配置

./configure

2.自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,须要在/var下建立temp及nginx目录

编译安装

make make install

查找安装路径:

whereis nginx
nginx-whereis.png

启动、中止nginx

cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload

启动时报80端口被占用:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

 解决办法:一、安装net-tool 包:yum install net-tools

 

./nginx -s quit:此方式中止步骤是待nginx进程处理任务完毕进行中止。
./nginx -s stop:此方式至关于先查出nginx进程id再使用kill命令强制杀掉进程。

查询nginx进程:

ps aux|grep nginx

重启 nginx

1.先中止再启动(推荐):
对 nginx 进行重启至关于先中止再启动,即先执行中止命令再执行启动命令。以下:

./nginx -s quit ./nginx

2.从新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效须要重启 nginx,使用-s reload不用先中止 ngin x再启动 nginx 便可将配置信息在 nginx 中生效,以下:
./nginx -s reload

启动成功后,在浏览器能够看到这样的页面:

nginx-welcome.png

开机自启动

即在rc.local增长启动代码就能够了。

vi /etc/rc.local

增长一行 /usr/local/nginx/sbin/nginx
设置执行权限:

chmod 755 rc.local

nginx-rclocal.png

到这里,nginx就安装完毕了,启动、中止、重启操做也都完成了,固然,你也能够添加为系统服务,我这里就不在演示了。

--------------------------------------分割线 --------------------------------------

Nginx负载均衡配置实战  http://www.linuxidc.com/Linux/2014-12/110036.htm

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

--------------------------------------分割线 --------------------------------------

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

 参考:

https://blog.csdn.net/wxyjuly/article/details/79443432

https://www.cnblogs.com/boonya/p/7907999.html