nginx服务器

1什么是Nginx

  Nginx 是一款高性能的 http 服务器(静态页面,jsp。htm)/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并且 cpu、内存等资源消耗却非常低,运行非常稳定。

  官方网站下载 nginxhttp://nginx.org/

 部署静态页面

 server {
        listen       87;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        }
 server {
        listen       88;
        server_name  127.0.0.1;

        location / {
            root   hrm-home;
            index  index.html index.htm;
        }
        }
 server {
        listen       99;
        server_name  127.0.0.1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   hrm-user;
            index  index.html index.htm;
        }
}

2作用,用处

  前端页面的部署

  解决端口访问问题

  反向代理  负载均衡 网关(后台代码和静态资源的切换)

  虚拟主机 把一台运行在互联网上的物理服务器划分成多个“虚拟”服务器。就是静态页面部署的时候,一个就静态页面部署一个主机

  (1修改服务名字

 location / {
            root   hrm_course_web;
            index  home.html;
        }      

  (2域名访问(虚拟主机步骤)

  nginx访问域名时候  会在本地去找  C:WindowsSystem32driversetchost  配置域名、

  然后再去dns域名解析器找

  (3 修改默认端口

server {
        listen       80;
        server_name  admin.hrm.com;
        location / {
            root   hrm_plat_web;
            index  index.html;
        }
    }
    server {
        listen       80;
        server_name  course.hrm.com;
        location / {
            root   hrm_course_web;
            index  home.html;
        }
    }

3反向代理

  正向代理 知道要访问的服务器

  反向代理:

    用一台服务器nginx,代理真实服务器,用户访问时,不再是访问真实服务器,而是代理服务器。

 

   配置反向代理

server {
        listen       80;
        server_name  course.hrm.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / { 
            proxy_pass http://127.0.0.1:6002; #访问域名为course.hrm.com都交给6002处理
            index  index.html index.htm home.html;
        }
    }

    server {
        listen       80;
        server_name  admin.hrm.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:6001; #访问域名为admin.hrm.com都交给6001处理
            index  index.html index.htm;
        }
    }

什么是反向代理?

代理:通过客户机的配置,实现让一台服务器代理客户机,客户的所有请求都交给代理服务器处理。

 

反向代理:用一台服务器,代理真实服务器,用户访问时,不再是访问真实服务器,而是代理服务器。

 

nginx可以当做反向代理服务器来使用:

我们需要提前在nginx中配置好反向代理的规则,不同的请求,交给不同的真实服务器处理

当请求到达nginxnginx会根据已经定义的规则进行请求的转发,从而实现路由功能

利用反向代理,就可以解决我们前面所说的端口问题,如图

原文地址:https://www.cnblogs.com/13438145925xiaozheng/p/11634911.html