Linux运维学习笔记之十四:Nginx服务

第二十三章 Nginx服务

一、Nginx基础
1、简介

Nginx ("enginex") 是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,国内使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。

2、功能

(1)web服务(http)

(2)负载均衡(反向代理proxy)

(3)web缓存(web cache)

3、优点

(1)高并发(静态小文件)。静态并发1-2万

(2)占用资源少。2万并发,开10个线程服务,内存消耗才几百M

(3)功能种类比较多(web、cache、proxy)。但每个功能都不是特别强

(4)支持epoll模型,使得nginx可以支持高并发

(5)nginx配合动态服务和apache有区别

(6)利用nginx可以对IP限速,也可以限制连接数

(7)配置简单、灵活

4、LNMP实现原理和架构(Linux Nginx MySQL PHP)
(1)LAMP实现:

用户请求-->Apache(:80)-->php模块(libphp5.so)-->mysql

(2)LNMP实现:

用户请求-->Nginx(:80)-->php服务(FCGI:9000 )-->mysql

当访问php页面时, Nginx通过配置文件是指定的php解析器,用fastcgi_passu将请求以http ip:9000的形式抛给FCGI解析.

5、Nginx的应用场合
(1)静态服务器

主要是图片、视频等服务,文件后缀名主要是.html、.js、css、flv等。另一个常用工具是lighttpd(百度贴吧在用)。并发一般是1-3万

(2)动态服务

主要是nginx+FaseCGI的方式运行php、jsp等程序。并发一般在500-1500.主要的瓶颈是在FaseCG和MySQL。FaseCGI的并发一般在500-1500,MySQL的并发一般是300-1500。相当于Apache+php或lighttpd+FCGI php

(3)反向代理(负载均衡)

日PV在2000万以下的,都可以直接用Nginx做代理。竞争对手是haproxy(软件)、F5(硬件)、A10(硬件)。

(4)缓存服务

相当于Squid或Varnish。

6、主流WEB服务产品对比
(1)Apache

a、2.2版本非常稳定强大,据官方说2.4版本性能超强。

b、Prefork模式取消了进程创建开销,性能很高。

c、处理动态业务数据时,因关联到后端的引擎和数据库,瓶颈不在于Apache本身。

d、基于传统的select模型,高并发时消耗系统的资源相对多一些。

e、扩展库采用DSO方法。如通过apxs安装php模块

h、功能多、插件多、更稳定、更安全

(2)Nginx

a、基于异步IO模型(epoll、kqueue),性能强,能支持上万并发。

b、对于小文件支持很好,性能很高(限静态小文件1M)。

c、代码优美,扩展库必须编译进主程序。

d、系统的资源比较低。

e、

(3)Lighttpd(百度贴吧和豆瓣网在用)

a、基于异步IO模型,性能和Nginx相近。

b、扩展库采用SO模式,比Nginx要灵活。

c、全球使用率较低,安全性没有Apache和Nginx好。

d、通过插件(mod_secdownload)可实现文件URL地址加密。

7、主流WEB服务性能对比
(1)静态数据性能对比

处理静态文件Apache性能比Nginx和lighttpd要差。在处理小文件(小于1M), Nginx和lighttpd比Apache优势明显,lighttpd最强。处理大于1M的文件,Apache、Nginx和lighttpd差不多

(2)动态数据性能对比

处于纯动态文件时,三者相差不大,主要是取决于PHP和数据库的处理性能。Nginx并不是什么都强,在处理大文件和动态数据时优势不明显。相比而言,Apache更有优势一点。这是因为处理动态数据的能力取决于PHP和后端数据库的服务能力,瓶颈不在WEB服务器上。一般PHP引擎支持的并发参考值为300-1000,Java引擎的并发为300-1000,数据库的并发为300-1000。

8、为什么Nginx总体性能比Apache要高

Nginx使用的是最新的epoll(Linux2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid 、Memcached都采用的是epoll网络I/O模型。在处理大量连接的读写民,Apache所采用的select网络I/O模型比较低效。

9、常用模型
(1) PPC/TPC 模型

这两种模型思想类似,就是让每一个到来的连接一边自己做事去,别再来烦我 。只是 PPC 是为它开了一个进程,而 TPC 开了一个线程。可是别烦我是有代价的,它要时间和空间啊,连接多了之后,那么多的进程 / 线程切换,这开销就上来了;因此这类模型能接受的最大连接数都不会高,一般在几百个左右。

(2)select 模型

a、最大并发数限制

因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Select 模型的最大并发数就被相应限制了。自己改改这个 FD_SETSIZE ?想法虽好,可是先看看下面吧 …

b、效率问题

select 每次调用都会线性扫描全部的 FD 集合,这样效率就会呈现线性下降,把FD_SETSIZE 改大的后果就是,大家都慢慢来,什么?都超时了。

c、内核/用户空间内存拷贝问题(数据由内核拷贝到用户态)

如何让内核把 FD 消息通知给用户空间呢?在这个问题上 select 采取了内存拷贝方法。

(3)poll 模型

基本上效率和 select 是相同的, select 缺点的 2 和 3 它都没有改掉。

(4)Epoll模型

把其他模型逐个批判了一下,再来看看 Epoll 的改进之处吧,其实把 select 的缺点反过来那就是 Epoll 的优点了。

a、Epoll 没有最大并发连接的限制

上限是最大可以打开文件的数目,这个数字一般远大于 2048, 一般来说这个数目和系统内存关系很大 ,具体数目可以 cat /proc/sys/fs/file-max 察看。

b、效率提升

 Epoll 最大的优点就在于它只管你“活跃”的连接 ,而跟连接总数无关,因此在实际的网络环境中, Epoll 的效率就会远远高于 select 和poll 。

c、内存拷贝

 Epoll 在这点上使用了“共享内存 ”,这个内存拷贝也省略了。

(5)Epoll 为什么高效

首先回忆一下 select 模型,当有 I/O 事件到来时, select 通知应用程序有事件到了快去处理,而应用程序必须轮询所有的 FD 集合,测试每个 FD 是否有事件发生,并处理事件。

Epoll 的高效和其数据结构的设计是密不可分的。epoll_data 是一个union 结构体 , 借助于它应用程序可以保存很多类型的信息 :fd 、指针等等。有了它,应用程序就可以直接定位目标。Epoll 不仅会告诉应用程序有I/0 事件到来,还会告诉应用程序相关的信息,这些信息是应用程序填充的,因此根据这些信息应用程序就能直接定位到事件,而不必遍历整个FD 集合。

10、工作中如何正确选择WEB服务软件

(1)静态业务:高并发时采用Nginx或lighttpd。根据自己的掌握程序或公司的要求选择。

(2)动态业务:采用Nginx和Apache均可。

(3)静动给合业务:Nginx或Apache选一个,不要多选,要单选。

(4)动态业务可以由前端代理(haproxy),根据页面元素的类型,向后转发相应的服务器进行处理。

(5)如果并发不是很大,又对Apache很熟悉,采用Apache也可以.Apache2.4很强大,并发连接数也有所增加。

(6)提示:Nginx做WEB(Apache、lighttpd)、反向代理(haproxy、lvs、nat)及缓存服务器(squid)。

(7)最终建议:如没什么特殊需求,对外业务用Nginx,对内业务用Apache(yum httpd、mysql、php).

 

11、Nginx虚拟主机

(1)一个server标签就是一个虚拟主机。(Apache是VirtualHost标签)

(2)基于域名的虚拟主机。通过域名来区分虚拟主机,主要应用于外部网站。

(3)基于端口的虚拟主机。通过端口来区分虚拟主机,主要应用于内部网站,或网站后台。

(4)基于IP的虚拟主机。通过IP来区分虚拟主机,几乎不用,不支持ifconfig别名,配置文件可以。

二、Nginx安装
1、安装需要的包
(1)pcre(Perl CompatibleRegular Expressions)

pcre的中文意思是perl兼容正则表达式。官方下载站点为http://www.pcre.org。安装pcre库是为了使Nginx支持Http Rewrite模块(伪静态)。

yum install pcre pcre-devel -y

安装过程:

a、解压

tar  xf pcre-8.30.tar.gz

b、configure配置参数

./configure --enable-utf8

c、编译

make

d、安装

make install

(2)openssl

yum install openssl openssl-devel -y

2、安装Nginx
(1)下载

wget http://nginx.org/download/nginx-1.6.2.tar.gz

(2)解压

tar xf nginx-1.6.2.tar.gz

(3)创建nginx用户

useradd nginx -M -s /sbin/nologin

(4)配置参数

cd nginx-1.6.2

./configure --help

./configure \

--user=nginx \

--group=nginx \

--prefix=/application/nginx1.6.2 \

--with-http_stub_status_module \

--with-http_ssl_module

说明:

    如果使用的是源码编译安装的pcre,则在配置nginx参数时,要通过--with-pcre参数来指定pcre的安装目录。如:--with-pcre=/usr/pcre-9.30

 

(5)编译

make

echo $?

(6)安装

make install

echo $?

(7)创建软连接

ln -s /application/nginx1.6.2/ /application/nginx

cd /application/nginx

(8)检查语法

/application/nginx/sbin/nginx -t

nginx: the configuration file/application/nginx1.6.2/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx1.6.2/conf/nginx.conftest is successful

(9)启动Nginx

/application/nginx/sbin/nginx

(10)检查监听端口

netstat -lntup | grep nginx

tcp          0         00.0.0.0:80          0.0.0.0:*       LISTEN      24896/nginx

lsof -i :80

COMMAND   PID  USER  FD   TYPE DEVICE SIZE/OFF NODENAME

nginx   24896  root   6u  IPv4 144008      0t0 TCP *:http (LISTEN)

nginx   24897 nginx    6u IPv4 144008      0t0  TCP *:http (LISTEN)

(11)打开页面检查

http://192.168.93.148/

(12)如果有问题,则可在服务端使用curl,确定web服务是否有问题

curl 127.0.0.1

(13)搭建默认站点

a、Nginx默认站点目录为安装目录下的html目录

grep html /application/nginx/conf/nginx.conf

  root   html;

  index  index.html index.htm;

b、修改默认的index.html文件

vim /application/nginx/html/index.html

三、Nginx基本配置
1、关于Nginx模块
(1)核心模块(Nginx Core Modules)

包括main和event

(2)标准模块(Standard Http Modues)

虽然不是必需的,但Nginx缺省都会安装,不建议改动。包括以下:

Core

Access

FastCGI

Gzip:压缩模堞,性能优化

Log:日志模块

Proxy:代理模块

Rewrite:URL重写模块,伪静态

Upstream:负载均衡模块

(3)说明

缺省条件下,以上两组模块都会安装,修改配置文件,主要是根据这两组模块的功能来修改模块的参数。通过官方文档http://nginx.org/en/docs/查看模块的使用

2、Nginx目录结构

+-- client_body_temp:客户端内容的临时文件

+-- conf

¦   +-- fastcgi.conf:动态文件的配置文件

¦   +-- fastcgi.conf.default:动态文件的配置文件的备份文件

¦   +-- fastcgi_params:动态文件的配置文件的参数文件

¦   +-- fastcgi_params.default:动态文件的配置文件的参数文件的备份文件

¦   +-- nginx.conf:nginx的核心配置文件,针对静态文件

¦   +-- nginx.conf.default:nginx的核心配置文件的备份文件

+-- html:Nginx的默认站点目录

¦   +-- 50x.html:错误页面

¦   +-- index.html

+-- logs

¦   +-- access.log

¦   +-- error.log

¦   +-- nginx.pid

+-- sbin

¦   +-- nginx:命令文件

3、Nginx主配置文件nginx.conf

Nginx的配置文件是以块的形式组织的,每个块以一个大括号“{}”来表示,块可以分为多个层次。整个配置文件中Main指令位于最高层,在Main层下面可以有Events、Http等层次,而在Http层中双包含Server层,即Server block,Server block中又可以分为location层,且可以包含多个location block。每一行都要以分号“;”结尾。

cat nginx.conf

#user  nobody;  # 默认用户,前面安装已改为nginx

worker_processes  1;  # 指定子进程,一般与CPU的核数一样

#error_log  logs/error.log;  #一般指定记录级别为error。记录少一些。logs/error.logerror;

#error_log logs/error.log  notice;

#error_log logs/error.log  info;

#pid        logs/nginx.pid;  # nginx的pid

events {  # 可以理解为nginx可以处理连接的最大数量

    worker_connections  1024; # worker的连接数

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #记录日志的格式

    #log_format  main '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent"$http_referer" '

    #                  '"$http_user_agent""$http_x_forwarded_for"';

    #access_log  logs/access.log  main; #是否记录访问日志

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       80; # 监听的端口

        server_name  localhost; #域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {   #根据前面的结果,执行下面的任务,做URL跳转

            root   html;

            index  index.html index.htm;

        }

        #error_page  404              /404.html;

        # redirect servererror pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html; #出现50开头的错误,跳转到指定的页面

        location = /50x.html {  #针对location指定的页面,再下面路径找

            root   html;

        }

       。。。。。。

}

4、Nginx动态文件配置文件fastcgi.conf

该配置文件主要是获取一些信息,一般不需要改动

四、Nginx配置实战:基于域名
1、简化nginx.conf配置文件(去注释和空行)

grep -Ev '#|^$' nginx.conf.default > nginx.conf 

2、修改配置文件

worker_processes  2;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  www.abc.org;

            root   html/www;

            index  index.html index.htm;

}

    server {

        listen       80;

        server_name  bbs.abc.org;

            root   html/bbs;

            index  index.html index.htm;

    }

    server {

        listen       80;

        server_name  blog.abc.org;

            root   html/blog;

            index  index.html index.htm;

    }

}

3、检查语法

/application/nginx/sbin/nginx -t

nginx: the configuration file/application/nginx1.6.2/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx1.6.2/conf/nginx.conftest is successful

4、创建相应站点目录

mkdir /application/nginx/html/{www,bbs,blog}

5、创建相应首页文件

for n in www bbs blog;do echo"$n.abc.org">/application/nginx/html/$n/index.html;done 

for n in www bbs blog;do cat/application/nginx/html/$n/index.html;done             

www.abc.org

bbs.abc.org

blog.abc.org

6、平滑重启nginx

/application/nginx/sbin/nginx -s reload

7、配置客户端hosts

192.168.188.218 www.abc.org bbs.abc.org blog.abc.org

8、测试nginx

http://www.abc.org

http://bbs.abc.org/

http://blog.abc.org/

9、Nginx配置虚拟主机流程

(1)复制完整的server标签段,粘贴到http标签里(一定要放在http的结束大括号之内)。

(2)更改server_name及对应网页的root根目录。

(3)检查配置文件的语法,平滑重启服务

(4)创建server_name对应的网页文件,并建立测试文件。没有index首页会报403错误

(5)在客户端hosts文件中添加对server_name的DNS解析

(6)在客户端的浏览器访问。

10、Nginx状态监控
(1)在配置文件nginx.conf中增加状态配置

server {

        listen       80;

        server_name  blog.abc.org;

            root   html/blog;

            index  index.html index.htm;

    }

##status

   server {

       listen       80;

       server_name  status.abc.org;

           stub_status on;

           access_log off;

   }

(2)检查语法

nginx -t

(3)平滑重启nginx

nginx -s reload

(4)配置客户端hosts

192.168.188.218 www.abc.org bbs.abc.org blog.abc.org status.abc.org

(5)访问状态页面

http://status.abc.org

Active connections: 4 
server accepts handled requests
 15 15 88 
Reading: 0 Writing: 1 Waiting: 3 
(6)状态页面内容说明

Active connections: 4

表示nginx正在处理的活动连接数为4个

server accepts handled requests

15 15 88 

第一个server表示nginx启动到现在共处理了15个连接

第二个accepts表示启动到现共成功创建了15次握手

第三个handled requests表示总共处了88次请求

请求丢失数=accepts(握手数) - server(连接数)

Reading: 0 Writing: 1 Waiting: 3

Reading:表示读取到客户端的Header信息数

Writing:表示返回给客户端的Header信息数

Waiting:已经处理完,正在等待下一次请求指令的驻留连接(开启keep-alive的情况下)。

waiting = Active -(Reading+ Writing)

11、查看worker_processes

由于前面已将worker_processes参数值改为2,系统应产生2个worker process

ps -ef | grep nginx

root      3783     1  014:09 ?        00:00:00 nginx: masterprocess sbin/nginx

nginx     4977  3783  015:57 ?        00:00:00 nginx: worker process

nginx     4978  3783  015:57 ?        00:00:00 nginx: worker process

五、Nginx企业实战
1、别名应用
(1)修改配置文件nginx.conf,添加别名

server {

        listen       80;

        server_name  www.abc.org abc.org;

            root   html/www;

            index  index.html index.htm;

}

(2)检查语法

nginx -t

(3)平滑重启nginx

nginx -s reload

(4)配置客户端hosts

192.168.188.218 www.abc.org bbs.abc.org blog.abc.org status.abc.org abc.org

(5)访问页面

http://abc.org/  #地址栏不变

2、301跳转(IP访问跳转到域名):URL地址重写
(1)修改配置文件nginx.conf,新增server,并添加rewrite(放在第一位)

server {

        listen       80;

        server_name  abc.org;

        rewrite ^/(.*) http://bbs.abc.org/$1 permanent;

}

(2)检查语法

nginx -t

(3)平滑重启nginx

nginx -s reload

(4)配置客户端hosts

192.168.188.218 www.abc.org bbs.abc.org blog.abc.org status.abc.org abc.org

(5)访问页面

http://abc.org/  #地址栏变为http://bbs.abc.org

http://192.168.93.148/  #地址栏变为http://bbs.abc.org,因为配置的是域名访问,没有配置IP访问,所以用IP来访问时,直接显示第一个server标签的内容,就跳转了。

3、防止恶意域名绑定
(1)修改配置文件nginx.conf,新增server,并添加location(放在第一位)

server {

        listen       80;

        location / {

            deny all;  #禁止访问,所有不符合server的域名都不能访问(如IP,其它域名)

    }

}

(2)检查语法

nginx -t

(3)平滑重启nginx

nginx -s reload

(4)访问页面

http://192.168.93.148/  #403错误

4、根据http状态码显示相关错误页面

server {

        listen       80;

        server_name  abc.org;

        location / {

            root html/www

            index index.html

        }

        error_page 403 /403.html      #此处的/403.htmlhtml/www下的403.html

        error_page 404 /404.html

        error_page 500 /500.html

        error_page 501 502 503 504 /50x.html

        location /50x.html {

            root html

        }

}

 

 

六、Nginx日志功能
1、修改配置文件nginx.conf,添加日志功能

error_loglogs/error.log error;     #添加错误日志功能

worker_processes  2;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

keepalive_timeout  65;

#定义访问日志格式

    log_format main  '$remote_addr - $remote_user[$time_local] "$request" '

                      '$status $body_bytes_sent"$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

    server {

        listen       80;

        server_name  www.abc.org;

            root   html/www;

            index  index.html index.htm;

            access_loglogs/www_access.log main;  #server中打开访问日志

    }

    server {

        listen       80;

        server_name  bbs.abc.org;

            root   html/bbs;

            index  index.html index.htm;

            access_loglogs/bbs_access.log main;  #server中打开访问日志

    }

    server {

        listen       80;

        server_name  blog.abc.org;

            root   html/blog;

            index  index.html index.htm;

            access_loglogs/blog_access.log main;  #server中打开访问日志

    }

}

2、查看logs目录下日志,没有www_access.log等文件

ll /application/nginx/logs/

-rw-r--r-- 1 root root   1 Mar10 06:33 access.log

-rw-r--r-- 1 root root 570 Mar 10 06:37 error.log

-rw-r--r-- 1 root root   5 Mar10 06:37 nginx.pid

3、平滑重启nginx

/application/nginx/sbin/nginx

4、访问http://www.abc.org
5、查看logs目录下日志,已生成www_access.log等文件

ll /application/nginx/logs/

-rw-r--r-- 1 root root   1 Mar10 06:33 access.log

-rw-r--r-- 1 root root   0 Mar10 06:37 bbs_access.log

-rw-r--r-- 1 root root   0 Mar10 06:37 blog_access.log

-rw-r--r-- 1 root root 570 Mar 10 06:37 error.log

-rw-r--r-- 1 root root   5 Mar10 06:37 nginx.pid

-rw-r--r-- 1 root root 436 Mar 10 06:37 www_access.log

6、查看生成的日志

cat /application/nginx/logs/www_access.log

192.168.188.116 - - [10/Mar/2017:06:37:55 +0800] "GET/favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101Safari/537.36" "-"

7、Nginx日志格式说明
(1)日志格式

log_format main  '$remote_addr - $remote_user[$time_local] "$request" '

                     '$status $body_bytes_sent "$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

(2)格式说明

$remote_addr:直接客户端地址

$remote_user:远程客户端用户名称

$time_local:访问时间和时区

$request:用户的请求,使用的Http协议

$status: 返回状态,如200、304、404等

$body_bytes_sent:发送的body字节数

$http_referer:引用页(从哪个链接访问来的)

$http_user_agent:客户端浏览器类型、版本等信息

$http_x_forwarded_for:间接客户端地址(一般前面会有代理服务器)

8、Nginx日志轮询

Nginx的日志轮询,一般采用cron +mv + reload的方式进行日志切割。

(1)生成移动日志的脚本文件

vi /server/scripts/cut_nginx_log.sh

/application/nginx/logs && \

/bin/mv www_access.log www_access_$(date +%F -d -1day).log

/application/nginx/sbin/nginx -s reload

(2)配置cron定时任务

crontab -e

00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh >/dev/null2>&1

(3)检查cron配置

crontab -l | tail -2

七、Nginx配置实战:基于端口
1、修改配置文件

worker_processes  2;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       8001;

        server_name  www.abc.org;

            root   html/www;

            index  index.html index.htm;

    }

    server {

        listen       8002;

        server_name  bbs.abc.org;

            root   html/bbs;

            index  index.html index.htm;

    }

    server {

        listen       8003;

        server_name  blog.abc.org;

            root   html/blog;

            index  index.html index.htm;

    }

}

2、检查语法

/application/nginx/sbin/nginx -t

3、平滑重启nginx

/application/nginx/sbin/nginx -s reload

4、测试nginx

http:// www.abc.org:8001

http://bbs.abc.org:8002

http://blog.abc.org:8003

八、Nginx配置实战:基于IP
1、在eth0上新增IP

ifconfig eth0:1 192.168.93.247/24 up

2、修改配置文件nginx.conf

worker_processes  2;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       192.168.93.148:80;

        server_name  www.abc.org;

            root  html/www;

            index  index.html index.htm;

    }

    server {

        listen       192.168.93.247:80;

        server_name  bbs.abc.org;

            root   html/bbs;

            index  index.html index.htm;

    }

}

2、检查语法

/application/nginx/sbin/nginx -t

3、平滑重启nginx

/application/nginx/sbin/nginx -s reload

4、测试nginx

http://192.168.93.148

http://192.168.93.247

九、Nginx配置文件优化
1、创建扩展配置文件目录

mkdir /application/nginx/conf/extra

2、创建扩展配置文件

vi /application/nginx/conf/extra/www.conf

server {

        listen       80;

        server_name  www.abc.org;

            root   html/www;

            index  index.html index.htm;

}

 

vi /application/nginx/conf/extra/bbs.conf

server {

        listen       80;

        server_name  bbs.abc.org;

            root   html/bbs;

            index  index.html index.htm;

}

 

vi /application/nginx/conf/extra/blog.conf

server {

        listen       80;

        server_name  blog.abc.org;

            root   html/blog;

            index  index.html index.htm;

}

3、修改主配置文件nginx.conf

error_log logs/error.log error; 

worker_processes  2;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #定义访问日志格式

    log_format  main '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status$body_bytes_sent "$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

    include extra/www.conf;

    include extra/bbs.conf;

    include extra/blog.conf;

}

4、检查语法

/application/nginx/sbin/nginx -t

5、平滑重启nginx

/application/nginx/sbin/nginx -s reload

6、测试nginx

http://www.abc.org

http://bbs.abc.org/

http://blog.abc.org/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值