快速掌握Nginx(一) —— 安装Nginx和简单配置虚拟主机
Nginx安装和简单配置虚拟主机
1 Nginx简介
Nginx是近几年最火热的http、反向代理服务器,百度阿里等互联网公司也都在使用Nginx,它也可以用作邮件代理服务器、TCP/UDP代理服务器等。Nginx功能和Apache相似,其优点主要在于以下几点:
①高并发响应性能好,官方nginx处理静态文件并非为5w/s; ②反向代理性能好(可用于负载均衡); ③内存和cpu占用率低(为Apache的1/5~1/10)。
Nginx是由内核和模块组成,内核设计十分简洁,实现的功能仅仅是:通过查找配置文件将客户端请求映射到一个location block(loaction是nginx配置中的一个指令,用于url匹配),location中所配置的每个指令都会启动不同的模块去完成相应的工作。
2 Nginx安装
这里我们把Nginx安装在Centos虚拟机上,安装十分简单,按照下边的命令即可安装成功(下载地址不是稳定的,按照需求更改wget后的下载链接 ,官方网址http://nginx.org/en/download.html):
#按装一些工具,pcre用于支持rewrite yum install pcre-devel pcre -y yum -y install openssl openssl-devel #切换到root,下载并解压 cd /usr/src wget -c http://120.52.51.15/nginx.org/download/nginx-1.14.2.tar.gz tar -xzf nginx-1.14.2.tar.gz cd nginx-1.14.2.tar.gz
#查看配置 ./configure --help
#预编译,设置用户wyy1和组wyyy,编译位置为/usr/local/nginx ,添加两个http相关模块 ./configure --user=wyy1 --group=wyyy --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#编译并安装 make &&make install
安装完成后,转到安装目录 cd /usr/local/nginx ,看到Nginx基本结构如下:
#最简单的配置 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #default server server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } #www.mysite1.com server { listen 80; server_name www.mysite1.com; location / { root /usr/local/nginx/html/mysite1/; index index.html index.htm; } } #www.mysite2.com server { listen 80; server_name www.mysite2.com; location / { root /usr/local/nginx/html/mysite2/; index index.html index.htm; } } }View Code 这里我们添加了两台虚拟主机www.mysite1.com和www.mysite2.com,www.mysite1.com主机对应的网站目录为 /usr/local/nginx/html/mysite1/ ,该目录下有index.html文件,内容如下图。mysite2也是一样的结构。
在我们的电脑上打开 C:\Windows\System32\drivers\etc 目录,修改该目录下的hosts文件,添加两个DNS映射(简单说明:添加DNS映射后,输入www.mysite1.com就会被解析为IP地址192.160.70.132。):
192.168.70.132 www.mysite1.com 192.168.70.132 www.mysite2.com
然后打开浏览器访问www.mysite1.com和www.mysite2.com,结果如下:
我们看到mysite1和mysite2都可以正常访问,说明我们的添加的虚拟主机可以正常运行。但是这样有一个问题,如果我们部署了几十个、甚至几百个虚拟主机,那么就要在nginx.conf中添加很多的server节点,如果我们想修改其中某一个server节点,查找和修改起来就比较麻烦,而且也容易误操作。我们可以给每个虚拟主机配置一个配置文件,这样修改某一个虚拟主机的配置就很方便了。
针对上边的栗子,首先我们创建目录 /usr/local/nginx/conf/vhosts ,这个目录用于存放各个虚拟主机的配置,命令如下:
mkdir /usr/local/nginx/conf/vhosts; cd /usr/local/nginx/conf/vhosts; vim www.mysite1.com;
我们让配置文件的名字和虚拟主机名一致,如www.mysite1.com虚拟主机的配置文件是 vhosts/www.mysite1.com文件,其内容如下:
#www.mysite1.com server { listen 80; server_name www.mysite1.com; location / { root /usr/local/nginx/html/mysite1/; index index.html index.htm; } }
同样www.mysite2.com虚拟主机的配置文件就是 vhosts/www.mysite2.com文件,其内容如下:
#www.mysite2.com server { listen 80; server_name www.mysite2.com; location / { root /usr/local/nginx/html/mysite2/; index index.html index.htm; } }
/usr/local/nginx/conf/nginx.conf 配置文件的内容修改如下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #加载vhosts目录下所有配置 include vhosts/* ;
执行 /usr/local/nginx/sbin/nginx -s reload 命令重启nginx,然后访问www.mysite1.com和www.mysite2.com均可正常访问。
4 Ngnix平滑升级
最后补充一下Nginx的升级。如果我们想将Ngnix升级到新版本,但是Nginx已经部署了很多的项目,怎么才能升级Ngnix而不影响我们以前部署的项目呢?这里不再多赘述,完整的升级命令如下:
### Nginx升级 pkill-9 ngnix ;
cd /usr/src; #①下载新版本 wget -c http://120.52.51.15/nginx.org/download/nginx-1.xx.xx.tar.gz #②获取旧版本的配置 /usr/local/nginx/sbin/nginx -V #③解压安装包 tar -xzf nginx-1.xx.xx.tar.gz cd nginx-1.xx.x.tar.gz #④预编译【一定要和旧版本的配置项一致】 ./configure --user=wyy1 --group=wyyy --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #⑤编译 make #⑥sbin的nginx替换成新版本的nginx mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old cp /usr/src/nginx-1.14.2/objs/nginx /usr/local/nginx/sbin/nginx
小结:这篇介绍Nginx在Centos上的安装和最简单的配置和添加虚拟主机,下一篇介绍location和rewrite部分。
- nginx安装,配置、虚拟主机、轮询、域名重定向
- Nginx安装配置(含PHP支持、虚拟主机、反向代理负载均衡)
- nginx安装,配置、虚拟主机、轮询、域名重定向
- LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)
- LNMP架构 (2)之 Nginx安装、默认虚拟主机、用户认证、域名重定向、配置文件详解
- nginx安装及配置虚拟主机
- 构建Nginx服务器之一 安装及虚拟主机配置
- Nginx安装及配置虚拟主机
- 简单实用nginx 配置 本机 虚拟主机 windows 下的。
- 如何快速掌握Nginx 安装与配置?这个方法可能有用!
- Nginx的安装与虚拟主机配置及HTTP和HTTPS共存配置
- linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
- nginx安装,配置、虚拟主机、轮询、域名重定向
- 全面掌握Nginx配置+快速搭建高可用架构 一 Centos7 安装Nginx
- 安装nginx、nginx配置虚拟主机、用户认证、域名重定向
- nginx介绍,nginx安装,配置nginx默认虚拟主机,nginx用户认证,域名重定向
- Linux上nginx安装配置(配置虚拟主机,静态资源)
- Nginx安装与配置:默认虚拟主机、用户认证和域名重定向
- nginx 的简单配置(虚拟主机、来源控制、https)
- Nginx安装及配置、负载均衡、反向代理、虚拟主机