美文网首页
nginx入门

nginx入门

作者: V锅锅 | 来源:发表于2018-09-27 16:38 被阅读0次

    一、安装nginx

    1、下载nginx

    http://nginx.org/download/nginx-1.8.1.tar.gz
    上传到Linux服务器,我这里是上传到/usr/tool/目录下
    进入tool文件夹下,解压上传的nginx压缩包

    [root@localhost /]# cd /usr/tool/
    [root@localhost tool]# tar -zxvf nginx-1.8.1.tar.gz

    2、准备环境

    需要安装gcc环境和相关第三方开发包,执行如下命令

    [root@localhost tool]#yum -y install gcc-c++
    [root@localhost tool]#yum install -y pcre pcre-devel
    [root@localhost tool]#yum install -y zlib zlib-devel
    [root@localhost tool]#yum install -y openssl openssl-devel

    命令解释
    yum -y install gcc-c++
    需要安装gcc的环境。

    yum install -y pcre pcre-devel
    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
    注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

    yum install -y zlib zlib-devel
    zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

    yum install -y openssl openssl-devel
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

    3、创建目录

    需要在/var下创建temp及nginx目录

    [root@localhost tool]#mkdir /var/temp/nginx/client -p

    4、Nginx编译

    [root@localhost /]# cd /usr/tool/nginx-1.8.1/
    [root@localhost nginx-1.8.1]# ./configure
    --prefix=/usr/tool/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

    5、安装Nginx:

    安装命令:make & make install

    [root@localhost nginx-1.8.1]# make & make install

    进入nginx目录,查看目录下有sbin文件夹,安装成功

    [root@localhost nginx-1.8.1]# cd /usr/tool/nginx
    [root@localhost nginx]# ll

    6、启动nginx

    进入sbin目录

    [root@localhost sbin]# ./nginx

    7、访问nginx

    输入安装nginx的IP,默认端口为80

    http://192.168.65.129/


    image.png

    如看到上图信息,nginx安装与启动成功

    二、配置虚拟主机

    nginx的配置文件在
    /usr/tool/nginx-1.8.1/conf/nginx.conf

    1、通过端口区分

    1.1、配置文件修改如下

    每个server监听1个端口
    修改完成需要每次配置文件修改之后都需要重新加载配置文件,命令如下

    [root@localhost nginx]# sbin/nginx -s reload

    #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;
            }
        }
        server {
            listen       81;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
            location / {
                root   html81;
                index  index.html index.htm;
            }
        }
    }
    

    1.2、复制文件夹用于81端口使用

    [root@localhost nginx]# cp html html81 -r

    修改html81目录下index.html内容来区分端口

    2、通过域名区分

    2.1、修改nginx的配置文件如下

    修改完刷新文件

    [root@localhost nginx]# sbin/nginx -s reload

    
    #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;
            }
    
        }
    
        server {
            listen       81;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html81;
                index  index.html index.htm;
            }
    
        }
    
        server {
            listen       80;
            server_name  www.test01.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   htmltest01;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  www.test02.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   htmltest02;
                index  index.html index.htm;
            }
        }
    }
    

    2.2、复制文件夹

    复制文件夹后并修改里面的index.html文件夹做区分

    [root@localhost nginx]# cp html htmltest01 -r
    [root@localhost nginx]# cp html htmltest02 -r

    2.3、修改本机hosts

    windows的hosts的文件在
    C:\Windows\System32\drivers\etc
    增加内容如下,ip换成安装nginx机器的地址

    192.168.65.129 www.test01.com
    192.168.65.129 www.test02.com

    2.4、浏览器访问

    浏览器输入
    http://www.test01.com/

    image.png

    浏览器输入
    http://www.test02.com/

    image.png

    至此,通过端口和域名区分虚拟主机设置完成

    三、nginx配置反向代理

    1、安装2个Tomcat

    我的Linux已经有1个Tomcat,复制2两份
    分别修改端口为8080和80801
    分别修改Tomcat下webapp目录下ROOT目录下的index.jsp以做区别
    然后分别启动2个Tomcat

    [root@localhost tomcat]# cp tomcat8 tomcat8-8080 -r
    [root@localhost tomcat]# cp tomcat8 tomcat8-8081 -r

    2、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;
    
       upstream tomcat8080 {
        server 192.168.65.129:8080;
        }
        server {
            listen       80;
            server_name  www.test01.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                proxy_pass   http://tomcat8080;
                index  index.html index.htm;
            }
        }
        upstream tomcat8081 {
        server 192.168.65.129:8081;
        }
        server {
            listen       80;
            server_name  www.test02.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                proxy_pass   http://tomcat8081;
                index  index.html index.htm;
            }
        }
    }
    

    3、浏览器访问

    http://www.test01.com/

    image.png

    http://www.test02.com/

    image.png

    反向代理配置成功

    四、nginx配置负载均衡

    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;
    
       upstream tomcat8080 {
        server 192.168.65.129:8080;
        server 192.168.65.129:8081;
        }
        server {
            listen       80;
            server_name  www.test01.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                proxy_pass   http://tomcat8080;
                index  index.html index.htm;
            }
        }
    
    }
    

    第一次访问http://www.test01.com/

    image.png

    第二次访问http://www.test01.com/

    image.png

    至此,nginx的负载均衡配置完成

    负载均衡的几种常用方式

    1、轮询(默认)

    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

    upstream tomcat8080 {
        server 192.168.65.129:8080;
        server 192.168.65.129:8081;
    }
    

    2、weight

    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的 情况。

    upstream tomcat8080 {
        server 192.168.65.129:8080 weight=3;
        server 192.168.65.129:8081 weight=7;
    }
    

    权重越高,在被访问的概率越大,如上例,分别是30%,70%。

    3、ip_hash指令

    上述方式存在一个问题就是说,在负载均衡系统中,假如用户在某台服务器上登录了,那么该用户第二次请求的时候,因为我们是负载均衡系统,每次请求都会重新定位到服务器集群中的某一个,那么已经登录某一个服务器的用户再重新定位到另一个服务器,其登录信息将会丢失,这样显然是不妥的。

    我们可以采用ip_hash指令解决这个问题,如果客户已经访问了某个服务器,当用户再次访问时,会将该请求通过哈希算法,自动定位到该服务器。

    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

    upstream tomcat8080 {
        ip_hash;
        server 192.168.65.129:8080;
        server 192.168.65.129:8081;
    }
    

    4、fair(第三方)

    按后端服务器的响应时间来分配请求,响应时间短的优先分配。

    upstream tomcat8080 {
        server 192.168.65.129:8080;
        server 192.168.65.129:8081;;
        fair;
    }
    

    5、url_hash(第三方)

    按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

    upstream tomcat8080 {
        server squid1:3128;
        server squid2:3128;
        hash $request_uri;
        hash_method crc32;
    }
    

    每个设备的状态设置为:
    1.down 表示单前的server暂时不参与负载
    2.weight 默认为1.weight越大,负载的权重就越大。
    3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
    4.fail_timeout:max_fails次失败后,暂停的时间。
    5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

    配置实例:

    #user  nobody;
    worker_processes  4;
    events {
        # 最大并发数
        worker_connections  1024;
    }
    http{
        # 待选服务器列表
        upstream tomcat8080 {
            # ip_hash指令,将同一用户引入同一服务器。
            ip_hash;
            server 192.168.65.129:8080 fail_timeout=60s;
            server 192.168.65.129:8081;
            }
    
        server{
                    # 监听端口
                    listen 80;
                    # 根目录下
                    location / {
                        # 选择哪个服务器列表
                        proxy_pass http://tomcat8080 ;
                    }
    
                }
    }
    

    相关文章

      网友评论

          本文标题:nginx入门

          本文链接:https://www.haomeiwen.com/subject/bayeoftx.html