Nginx简单介绍及安装

10 篇文章 0 订阅
7 篇文章 0 订阅

Nginx概述

Nginx(发音同engine x)是一款由俄罗斯程序员Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。此软件BSD-like协议下发行,可以在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及MicrosoftWindows等操作系统中运行。在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或 FreeBSD操作系统上采用类似于epoll的高效事件模型kqueue。

特点

        作为web服务器:处理静态文件,索引文件,自动索引的效率较高                             

        作为代理服务器:实现无缓存的反向代理加速,提高网站运行速度

        作为负载均衡服务器:内部支持Rails和PHP,也可以支持HTTP代理服务器对进行服务      

        性能方面: 采用epoli模型,可以支持更多的并发连接,量大的恶意支持5万和并发连接数的响应

        稳定性方面:才去分阶段资源分配技术,是的cpu与内存占用率非常低

        高可用方面:支持热部署,平滑升级,启动速度快

应用场景

        使用Nginx结合FastCGI运行PHP,JSP,Perl等程序

        使用Nginx反向代理,负载均衡,规则过滤

        使用Nginx运行静态HTML页,图片

        使用Nginx加cache插件实现对web服务器缓存功能

        Nginx与其他新技术的结合应用

Nginx应用瓶颈和注意事项

        Nginx模块需要用C语言开发,必须符合一系列的规则,且需要熟悉Nginx的源代码,大大增加开发难度

Nginx处理请求过程

        首先,Nginx启动时,会解析配置文件,得到需要监听的端口和IP地址,在Nginx的master进程里先初始化监控的socket,在进行listen,然后fork多个子进程,子进程会竞争accept新的链接,此时,客户端就可以想Nginx发起连接了。

        当客户端与Nginx进行三次握手,建立好一个链接后,某一个子进程会accept成功,然后进行Nginx对链接的封装,即ngx_connection_t结构体,此时根据时间调用相应的事件处理模块,如http模块与客户端进行数据交换。最后nginx或客户端来主动关闭链接。

Nginx的安装

包的获取

        RPM包:http://nginx.org/packages/

        源码包:http://nginx.org/download/

源码安装

[root@nginx ~]# useradd nginx
[root@nginx ~]# wget http://nginx.org/download/nginx-1.15.0.tar.gz
[root@nginx ~]# yum install gcc gcc-c++ make pcre-devel openssl openssl-devel -y
[root@nginx ~]# tar -xf nginx-1.15.0.tar.gz -C /usr/local/src/
[root@nginx ~]# cd /usr/local/src/nginx-1.15.0
[root@nginx src/nginx-1.15.0]# ./configure --prefix=/opt/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
[root@nginx nginx-1.15.0]# echo $?
0
[root@nginx nginx-1.15.0]# make && make install
[root@nginx nginx-1.15.0]# echo $? 
0
# 配置环境变量
[root@nginx nginx-1.15.0]# cd /opt/data/nginx/sbin/
[root@nginx sbin]# ./nginx -t
nginx: the configuration file /opt/data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/data/nginx/conf/nginx.conf test is successful
[root@nginx sbin]# vim /etc/profile.d/nginx.sh
export PATH=/opt/data/nginx/sbin:$PATH
[root@nginx sbin]# source /etc/profile.d/nginx.sh
# nginx配置
[root@nginx sbin]# cd /opt/data/nginx/conf
[root@nginx conf]# ln -s /opt/data/nginx/conf /etc/nginx
[root@nginx conf]# mv nginx.conf nginx.conf.bak
[root@nginx conf]# vim nginx.conf
    user nginx;
    pid /var/run/nginx.pid;
    events {
            worker_connections 1024;
    }
    http {
            access_log off;
            client_max_body_size 128M;
            include /etc/nginx/conf.d/*.conf;
    }
[root@nginx conf]# cd
[root@nginx ~]# mkdir /etc/nginx/conf.d
[root@nginx ~]# mkdir /var/log/nginx
[root@nginx ~]# chown -R nginx:nginx /var/log/nginx

# nginx启动
[root@nginx ~]# vim /lib/systemd/system/nginx.service
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    PIDFile=/var/run/nginx.pid
    ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
    ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true

yum安装

# 本次安装过程一直显示公钥问题,要把配置的源gpgcheck改为0,才能安装

# 下载nginx源 
# ls nginx-1.18.0-1.el7.ngx.x86_64.rpm 
# yum localinstall nginx-1.8.0-1.el7.ngx.x86_64.rpm 
# yum install nginx -y   # 此方法会给上面安装 的升级,需要注意公钥的问题

--配置文件 /etc/nginx/nginx.conf

--主目录 /usr/share/nginx/html/

--查看版本 /usr/sbin/nginx -v

--配置文件语法检查 /usr/sbin/nginx -t

--服务启动停止 /etc/init.d/nginx {start|stop|restart|reload|status}

注意:若需要gzip和rewrite的正则,需要zlib,zlib-devel,pcre已经安装好。

检查测试

nginx 虚拟主机

虚拟主机介绍

所谓的虚拟主机,在web服务器里就是一个独立的网络站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外服务供用户访问。

基于域名的虚拟主机

# cd /opt/data/nginx
# mkdir data
# cd data
# mkdir -p test1/basic
# mkdir -p test2/basic
# chown -R nginx:nginx test1/basic
# chown -R nginx:nginx test2/basic
# mkdir -p test1/log
# mkdir -p test2/log
# chown -R nginx:nginx test1/log/
# chown -R nginx:nginx test2/log/
# vim test1/basic/index.html
This is a test from www.test1.com
# vim test2/basic/index.html
This is a test from www.test2.com of 192.168.10.21

# cd /opt/data/nginx/conf/conf.d
# vim test1.conf
server {
    listen 192.168.10.21:80;
    server_name www.test1.com;
    access_log /opt/data/nginx/data/test1/log/access.log combined;
    location / {
      root /opt/data/nginx/data/test1/basic;
      index index.html index.htm;
    }
}
# vim test2.conf
server {
    listen 192.168.10.21:80;
    server_name www.test2.com;
    access_log /opt/data/nginx/data/test2/log/access.log combined;
    location / {
      root /opt/data/nginx/data/test2/basic;
      index index.html index.htm;
    }
}
# nginx -t   #检查语法

注:主机hosts文件:C:\Windows\System32\drivers\etc

192.168.10.21 www.test1.com 
192.168.10.21 www.test2.com

测试

 

基于端口的虚拟主机

# 创建域名对应的站点目录文件 
# cd /opt/data/nginx/conf/conf.d 
# vim test3.conf s
server {
     listen 192.168.10.21:6969;
     server_name www.test3.com;
     access_log /opt/data/nginx/data/test3/log/access.log combined;
     location / {
       root /opt/data/nginx/data/test3/basic;
       index index.html index.htm;
     } 
} 
# vim test4.conf 
server {
     listen 192.168.10.21:8080;
     server_name www.test4.com;
     access_log /opt/data/nginx/data/test4/log/access.log combined;
     location / {
       root /opt/data/nginx/data/test4/basic;
       index index.html index.htm;
     } 
} 
# cd /opt/data/nginx/data 
# cp -R test2 test3 
# cp -R test2 test4 
# vim test3/basic/index.html 
This is a test from www.test3.com of 192.168.10.21:6969 
# vim test4/basic/index.html 
This is a test from www.test4.com of 192.168.10.21:8080 
# echo "" > test3/log/access.log 
# echo "" > test4/log/access.log 
# chown  -R nginx:nginx test3/basic/ 
# chown  -R nginx:nginx test3/log 
# chown -R nginx:nginx test4/basic/ 
# chown -R nginx:nginx test4/log/ 
# systemctl restart nginx 
# 另一个主机加上个域名解析 
# vim /etc/hosts
192.168.10.21  www.test3.com 
192.168.10.21  www.test4.com

测试

基于虚拟ip的虚拟主机

# (1) 创建站点目录文件 
# cat test5.comf 
server {
     listen 10.10.10.11:80;
     server_name www.test5.com;
     access_log /opt/data/nginx/data/test5/log/access.log combined;
     location / {
       root /opt/data/nginx/data/test5/basic;
       index index.html index.htm;
     } 
} 
# cat test6.conf 
# cat test5.comf 
server {
     listen 10.10.10.11:80;
     server_name www.test6.com;
     access_log /opt/data/nginx/data/test6/log/access.log combined;
     location / {
       root /opt/data/nginx/data/test6/basic;
       index index.html index.htm;
     }
 } 


# cd /opt/data/nginx/data/ 
# cp -R -p test3 test5 
# cp -R -p test3 test6 
# echo "" > test5/log/access.log 
# echo "" > test6/log/access.log 
# vim test5/basic/index.html 
This is a test from www.test5.com of 10.10.10.11 
# vim test6/basic/index.html 
This is a test from www.test6.com of 10.10.10.21 
# 添加ip 
# ip addr add 10.10.10.11 dev ens33 
# ip addr add 10.10.10.21 dev ens33 
# 另一台机子添加域名解析和路由 
# cat /etc/hosts 10.10.10.11  
www.test5.com 10.10.10.21  
www.test6.com 
# ip route add  10.10.10.11 via 192.168.10.21 dev ens33 
# ip route add  10.10.10.21 via 192.168.10.21 dev ens33

------------------------------------------------------------------------------------------------------- 返回目录

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值