Nginx功能简介与基本配置总结

来源:互联网 发布:2017网络直播平台排名 编辑:程序博客网 时间:2024/06/06 20:31

(1)Nginx的介绍


简单来说Nginx就是一个高性能的http/反向代理 服务器及电子邮件(IMAP/POP3)代理服务器。官方测试Nginx能够支撑5万并发请求,并且cpu,内存等资源消耗非常低,运行非常稳定,这就是国内百度,淘宝,腾讯,网易等各大互联网公司用它的主要原因。     

应用场景

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

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

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


首先安装好Nginx,如果不懂怎么在Linux安装Nginx 可以前往我的另一篇博文:  在Linux上安装Nginx


(2)使用


1, 当做http服务器使用,也就是说我们通过 http://ip/资源  ,可以访问到里面的资源,注意是静态的资源,jsp,php那些是访问不到的,端口默认80。

在你安装的nginx目录下的conf 目录下的nginx.conf



这个文件就是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 {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

可以看到 server这个节点,默认帮我们配置了端口80,访问文件根目录 html, 这个html就是我们安装Nginx里面的一个文件夹



在地址栏上输入如下你的服务器ip,这样我们只需要将我们需要的静态资源拖进html里面访问就可以了。



2. 虚拟主机,也就是说一台服务器可以启动多个网站!

如何区别不同的网站?

1、端口不同

2、域名不同

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

    server {        listen       81;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html81;            index  index.html index.htm;        }    }
只需要在nginx.conf文件加上一个 server节点!更换下端口和访问的根目录,注意如果没有此根目录要先创建,不然访问不到。
最后记得重新加载下nginx的配置文件


进入 sbin,  执行 ./nginx -s reload。



2)域名不同配置虚拟主机

什么意思?简单来说,比如:www.4399.com 和 www.7k7k.com 都指向同一ip,都是访问同一个Nginx服务器,Nginx根据不同的这些域名访问不同的目录,也就对应不同的资源,不同的网站了。

注意:

一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。

本地测试可以修改hosts文件。

修改window的hosts文件:(C:\Windows\System32\drivers\etc)

可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。


首先,修改本地host(可以使用SwitchHosts软件,直接修改,不用去C盘慢慢找Hosts):



然后配置 nginx.conf ,添加一个server节点

    server {        listen       80;        server_name  www.4399.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html4399;            index  index.html index.htm;        }    }

最后 重新加载配置文件   执行   ./nginx -s reload。



3. 反向代理。

??? 什么反向代理。既然有反向代理就有正向代理。


正向代理:




如图所示:正向代理就好比 在局域网里的多台PC机上不了外网,但又要访问外网,这个时候PC机请求代理服务器,代理服务器将它们的请求转发到互联网,然后在将响应的内容返回给各个PC机。


反向代理:



如图所示:互联网发出一个请求,然后这个代理服务器,将请求转发给了其他的服务器。而这个代理服务器就是Nginx,比如我们有一台服务器 192.168.1.100,启动了Nginx

当我们访问 http://192.168.1.100 ,然后他却不是找 Nginx服务器里面的静态资源,而是反向给了对应的Tomcat服务器找对应的资源。



(3)Nginx实现反向代理


两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。
两个域名是www.4399.com.cn和www.7k7k.com
nginx服务器使用虚拟机 192.168.147.128


第一步:安装两个tomcat,分别运行在8080和8081端口。

第二步:启动两个tomcat。

第三步:反向代理服务器的配置 ( 添加如下的节点 )

upstream tomcat1 {server 192.168.147.128:8080;    }    server {        listen       80;        server_name  www.4399.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass   http://tomcat1;            index  index.html index.htm;        }    }    upstream tomcat2 {server 192.168.147.128:8081;    }    server {        listen       80;        server_name  www.7k7k.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass   http://tomcat2;            index  index.html index.htm;        }    }

第四步:nginx重新加载配置文件

第五步:配置域名

在hosts文件中添加域名和ip的映射关系



(4)负载均衡


如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

upstream tomcat2 {

server 192.168.147.128:8081;

server 192.168.147.128:8082;

}

 

可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

 upstream tomcat2 {server 192.168.147.128:8081;server 192.168.147.128:8082 weight=2; }