一、Nginx的应用场景

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

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

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


二、Nginx配置虚拟主机

(1)通过端口区分不同虚拟机

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf


#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;

        }

    }

  server {

        listen       81;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

    }


}

(2)通过域名区分虚拟主机

重点仍然是配置server节点,不过这里的端口不变,都是80端口,而server_name是不同的域名

    server {

        listen       80;

        server_name  e3mall.xin;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-taobao;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  e3bos.top;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-baidu;

            index  index.html index.htm;

        }

    }

}

三、Nginx实现反向代理

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。


反向代理服务器之nginx初识

Nginx的配置文件

upstream tomcat1 {

server 127.0.0.1:8080;

    }

    server {

        listen       80;

        server_name  e3bos,top;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat1;

            index  index.html index.htm;

        }

    }

    upstream tomcat2 {

server 127.0.0.1:8081;

    }

    server {

        listen       80;

        server_name  e3mall.xin;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat2;

            index  index.html index.htm;

        }

    }



铸剑团队签名:

【总监】十二春秋之,[email protected]

【Master】戈稻不苍,[email protected]

【Java开发】雨鸶,[email protected];思齐骏惠,[email protected]小王子,[email protected];巡山小钻风,[email protected]

【VS开发】豆点,[email protected]

【系统测试】土镜问道,[email protected];尘子与自由,[email protected]

【大数据】沙漠绿洲,[email protected];张三省,[email protected]

【网络】夜孤星,[email protected]

【系统运营】三石头,[email protected];平凡怪咖,[email protected]

【容灾备份】秋天的雨,[email protected]

【安全】保密,你懂的。

原创作者:小王子

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。



相关文章: