Nginx的应用场景与安装步骤

Nginx

1.1 什么是nginx

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。开源、免费。

1.2 Nginx的应用场景

1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

1.3 Nginx的安装步骤

第一步:把nginx的压缩包上传到服务器

第二步:解压。

第三步:创建一个makefile

参数设置如下:

扫描二维码关注公众号,回复: 869083 查看本文章

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/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下创建tempnginx目录

第四步:编译make

第五步:安装make install

1.4 Nginx实现虚拟机

可以实现在同一台服务运行多个网站,而且网站之间互相不干扰。

同一个服务器可能有一个ip,网站需要使用80端口。网站的域名不同。

区分不同的网站有三种方式:

1、ip区分

2、端口区分

3、域名区分

1.4.1 Ip区分虚拟主机

需要一台服务器绑定多个ip地址。

方法一:

使用标准的网络配置工具(比如ifconfigroute命令)添加lP别名:

当前ip配置情况:

eth0网卡再绑定一个ip192.168.101.103

/sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up

/sbin/route add -host 192.168.101.103 dev eth0:1

方法二:

1/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1

修改其中内容:

DEVICE=eth0:1

IPADDR=192.168.25.103

其他项不用修改

2、重启系统

1.4.2 配置nginx基于ip地址的虚拟主机

1.4.2.1 Nginx的配置文件

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

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 { #一个Server就是一个虚拟主机

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

}

1.4.2.2 基于ip的虚拟主机配置

server {

        listen       80;

        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-141;

            index  index.html index.htm;

        }

        

    }

    server {

        listen       80;

        server_name  192.168.25.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-100;

            index  index.html index.htm;

        }

        

    }

Nginx重新加载配置文件。

1.4.3 基于端口的虚拟主机

server {

        listen       81;

        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-81;

            index  index.html index.htm;

        }

        

    }

    server {

        listen       82;

        server_name  192.168.25.141;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-82;

            index  index.html index.htm;

        }

        

    }

1.4.4 基于域名的虚拟主机

最有用的虚拟主机配置方式。

一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定。

可以修改host文件实现域名访问。

1.4.4.1 设置域名和ip的映射关系

修改windowhosts文件:C:\Windows\System32\drivers\etc

1.4.4.2 基于域名的虚拟主机配置

server {

        listen       80;

        server_name  www.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-www;

            index  index.html index.htm;

        }

        

    }

    server {

        listen       80;

        server_name  hehe.itheima.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-hehe;

            index  index.html index.htm;

        }

        

    }

修改配置文件后,需要nginx重新加载配置文件。

猜你喜欢

转载自blog.csdn.net/baidu_20876831/article/details/80335518