• CentOS 7 安装nginx + 部署站点 + 反向代理 + nginx负载均衡


    CentOS 7 安装nginx及配置

    安装nginx

    1. 安装依赖库

    yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

    2. 下载源码包

    wget -c https://nginx.org/download/nginx-1.9.6.tar.gz

    3. 解压

    tar -zxvf nginx-1.9.6.tar.gz

    4. 配置,编译,安装,开启nginx状态监测功能

    ./configure --prefix=/opt/nginx196/ --with-http_ssl_module --with-http_stub_status_module 

    make && make install

    5. 启动nginx

    cd /opt/nginx196/sbin
    # 启动
    ./nginx
    
    # 关闭
    ./nginx -s stop
    
    # 平滑重启
    ./nginx -s reload
    nginx的configure参数
    下面是nginx源码程序的configure参数:
    
    --prefix= 指向安装目录。
    --sbin-path= 指定执行程序文件存放位置。
    --modules-path= 指定第三方模块的存放路径。
    --conf-path= 指定配置文件存放位置。
    --error-log-path= 指定错误日志存放位置。
    --pid-path= 指定pid文件存放位置。
    --lock-path= 指定lock文件存放位置。
    --user= 指定程序运行时的非特权用户。
    --group= 指定程序运行时的非特权用户组。
    --builddir= 指向编译目录。
    --with-rtsig_module 启用rtsig模块支持。
    --with-select_module 启用select模块支持,一种轮询处理方式,不推荐在高并发环境中使用,禁用:--without-select_module。
    --with-poll_module 启用poll模块支持,功能与select相同,不推荐在高并发环境中使用。
    --with-threads启用thread pool支持。
    --with-file-aio 启用file aio支持。
    --with-http_ssl_module 启用https支持。
    --with-http_v2_module 启用ngx_http_v2_module支持。
    --with-ipv6 启用ipv6支持。
    --with-http_realip_module 允许从请求报文头中更改客户端的ip地址,默认为关。
    --with-http_addition_module 启用ngix_http_additon_mdoule支持(作为一个输出过滤器,分部分响应请求)。
    --with -http_xslt_module 启用ngx_http_xslt_module支持,过滤转换XML请求 。
    --with-http_image_filter_mdoule 启用ngx_http_image_filter_module支持,传输JPEGGIFPNG图片的一个过滤器,默认不启用,需要安装gd库。
    --with-http_geoip_module 启用ngx_http_geoip_module支持,用于创建基于MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量。
    --with-http_sub_module 启用ngx_http_sub_module支持,允许用一些其他文本替换nginx响应中的一些文本。
    --with-http_dav_module 启用ngx_http_dav_module支持,增加PUT、DELETE、MKCOL创建集合,COPY和MOVE方法,默认为关闭,需要编译开启。
    --with-http_flv_module 启用ngx_http_flv_module支持,提供寻求内存使用基于时间的偏移量文件。
    --with-http_mp4_module 启用ngx_http_mp4_module支持,启用对mp4类视频文件的支持。
    --with-http_gzip_static_module 启用ngx_http_gzip_static_module支持,支持在线实时压缩输出数据流。
    --with-http_random_index_module 启用ngx_http_random_index_module支持,从目录中随机挑选一个目录索引。
    --with-http_secure_link_module 启用ngx_http_secure_link_module支持,计算和检查要求所需的安全链接网址。
    --with-http_degradation_module 启用ngx_http_degradation_module 支持允许在内存不足的情况下返回204或444代码。
    --with-http_stub_status_module 启用ngx_http_stub_status_module 支持查看nginx的状态页。
    --without-http_charset_module 禁用ngx_http_charset_module这一模块,可以进行字符集间的转换,从其它字符转换成UTF-8或者从UTF8转换成其它字符。它只能从服务器到客户端方向,只有一个字节的字符可以转换。
    --without-http_gzip_module 禁用ngx_http_gzip_module支持,同--with-http_gzip_static_module功能一样。
    --without-http_ssi_module 禁用ngx_http_ssi_module支持,提供了一个在输入端处理服务器包含文件(SSI)的过滤器。
    --without-http_userid_module 禁用ngx_http_userid_module支持,该模块用来确定客户端后续请求的cookies。
    --without-http_access_module 禁用ngx_http_access_module支持,提供了基于主机ip地址的访问控制功能。
    --without-http_auth_basic_module 禁用ngx_http_auth_basic_module支持,可以使用用户名和密码认证的方式来对站点或部分内容进行认证。
    --without-http_autoindex_module 禁用ngx_http_authindex_module,该模块用于在ngx_http_index_module模块没有找到索引文件时发出请求,用于自动生成目录列表。
    --without-http_geo_module 禁用ngx_http_geo_module支持,这个模块用于创建依赖于客户端ip的变量。
    --without-http_map_module 禁用ngx_http_map_module支持,使用任意的键、值 对设置配置变量。
    --without-http_split_clients_module 禁用ngx_http_split_clients_module支持,该模块用于基于用户ip地址、报头、cookies划分用户。
    --without-http_referer_module 禁用ngx_http_referer_modlue支持,该模块用来过滤请求,报头中Referer值不正确的请求。
    --without-http_rewrite_module 禁用ngx_http_rewrite_module支持。该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级别设置该选项,那么将在location之前生效,但如果location中还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么location部分会再次被执行作为新的URI,这个循环会被执行10次,最后返回一个500错误。
    --without-http_proxy_module 禁用ngx_http_proxy_module支持,http代理功能。
    --without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持,该模块允许nginx与fastcgi进程交互,并通过传递参数来控制fastcgi进程工作。
    --without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持,该模块用来使用uwsgi协议,uwsgi服务器相关。
    --without-http_scgi_module 禁用ngx_http_scgi_module支持,类似于fastcgi,也是应用程序与http服务的接口标准。
    --without-http_memcached_module 禁用ngx_http_memcached支持,用来提供简单的缓存,提高系统效率。
    --without-http_limit_conn_module 禁用ngx_http_limit_conn_module支持,该模块可以根据条件进行会话的并发连接数进行限制。
    --without-http_limit_req_module 禁用ngx_limit_req_module支持,该模块可以实现对于一个地址进行请求数量的限制。
    --without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持,该模块在内存中常驻了一个1*1的透明gif图像,可以被非常快速的调用。
    --without-http_browser_module 禁用ngx_http_browser_mdoule支持,创建依赖于请求报头的值 。如果浏览器为modern,则$modern_browser等于modern_browser_value的值;如果浏览器为old,则$ancient_browser等于$ancient_browser_value指令分配的值;如果浏览器为MSIE,则$msie等于1。
    --without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持,该模块用于简单的负载均衡。
    --with-http_perl_module 启用ngx_http_perl_module支持,它使nginx可以直接使用perl或通过ssi调用perl。
    --with-perl_modules_path= 设定perl模块路径
    --with-perl= 设定perl库文件路径
    --http-log-path= 设定access log路径
    --http-client-body-temp-path= 设定http客户端请求临时文件路径
    --http-proxy-temp-path= 设定http代理临时文件路径
    --http-fastcgi-temp-path= 设定http fastcgi临时文件路径
    --http-uwsgi-temp-path= 设定http scgi临时文件路径
    --http-scgi-temp-path= 设定http scgi临时文件路径
    --without-http 禁用http server功能
    --without-http-cache 禁用http cache功能
    --with-mail 启用POP3、IMAP4、SMTP代理模块
    --with-mail_ssl_module 启用ngx_mail_ssl_module支持
    --without-mail_pop3_module 禁用pop3协议。
    --without-mail_iamp_module 禁用iamp协议。
    --without-mail_smtp_module 禁用smtp协议。
    --with-google_perftools_module 启用ngx_google_perftools_mdoule支持,调试用,可以用来分析程序性能瓶颈。
    --with-cpp_test_module 启用ngx_cpp_test_module支持。
    --add-module= 指定外部模块路径,启用对外部模块的支持。
    --with-cc= 指向C编译器路径。
    --with-cpp= 指向C预处理路径。
    --with-cc-opt= 设置C编译器参数,指定--with-cc-opt="-I /usr/lcal/include",如果使用select()函数,还需要同时指定文件描述符数量--with-cc-opt="-D FD_SETSIZE=2048"。 (PCRE库)
    --with-ld-opt= 设置连接文件参数,需要指定--with-ld-opt="-L /usr/local/lib"。(PCRE库)
    --with-cpu-opt= 指定编译的CPU类型,如pentium,pentiumpro,...amd64,ppc64...
    --without-pcre 禁用pcre库。
    --with-pcre 启用pcre库。
    --with-pcre= 指向pcre库文件目录。
    --with-pcre-opt= 在编译时为pcre库设置附加参数 。
    --with-md5= 指向md5库文件目录。
    --with-md5-opt= 编译时为md5库设置附加参数。
    --with-md5-asm 使用md5汇编源。
    --with-sha1= 指向sha1库文件目录。
    --with-sha1-opt= 编译时为sha1库设置附加参数。
    --with-sha1-asm 使用sha1汇编源。
    --with-zlib= 指向zlib库文件目录。
    --with-zlib-opt= 在编译时为zlib设置附加参数。
    --with-zlib-asm= 为指定的CPU使用汇编源进行优化。
    --with-libatomic 为原子内存的更新操作的实现提供一个架构。
    --with-libatomic= 指向libatomic_ops的安装目录。
    --with-openssl= 指向openssl安装目录。
    --with-openssl-opt= 在编译时为openssl设置附加参数。
    --with-debug 启用debug日志。
    
    nginx的configure参数
    nginx的configure配置参数

    6. 安装完成后测试

    netstat -tunlp|grep 80
    ps -ef|grep nginx

    nginx的目录结构

    [root@rpw nginx196]# ll
    drwxr-xr-x 2 root   root 4096 Dec 27 10:41 conf
    drwxr-xr-x 2 root   root 4096 Dec 27 16:26 html
    drwxr-xr-x 2 root   root 4096 Dec 27 10:22 logs
    drwxr-xr-x 2 root   root 4096 Dec 27 10:20 sbin
    [root@rpw nginx196]# 
    • conf 存放nginx所有的配置文件,主要是nginx.conf
    • html 存放nginx默认站点的目录,index.html.error.html...
    • logs 存放nginx默认日志的目录,如error.log,access.log
    • sbin 存放nginx主命令的目录, ./nginx

    部署一个web站点

    1. nginx默认站点是nginx目录下的html/index.html,可以在 conf/nginx.conf配置文件下查看

    server {
            listen       80;
            server_name  www.rpw.com; # 域名,记得在本地的win机器上修改hosts文件
            location / {
                root   html; # 默认的站点html文件夹,如 /opt/nginx196/html/
                index  index.html index.htm; # 在html目录下的站点首页文件
            }
        }

    # win10本地文件夹路径
    C:WindowsSystem32driversetchosts

    2. 通过访问 www.rpw.com 域名可以访问 index.html网页

    nginx虚拟主机

    1. 虚拟主机就是将一台服务器分割成多个虚拟服务器,每个站点使用各自的硬盘空间,节省资源.

    2. 每个server{}代码块都是一个虚拟主机.只要是来自www.rpw.com的请求,都会被转发给对应的server{}代码块

    #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 rpw_upstream {
            server 120.78.234.181;
            #server 10.0.3.126:8080;
            server 10.0.3.161;
        }    
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                #proxy_pass http://rpw_upstream;
                root   html;
                index  index.html index.htm;
            }
    
            error_page  404              /404.html;
            location = /404.html {
                root 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;
            #}
        }
        server {
            listen       80;
            server_name  www.rpw0.com;
    
            location / {
                proxy_pass http://rpw_upstream;
    
                #allow 10.0.3.47;
                #allow 10.0.3.158;
                #allow 10.0.3.126;
                #deny 10.0.3.47;
                
                root   /opt/web_nginx/rpw0;
                index  index.html index.htm;
            
            location /status {
                stub_status on;
            }
            error_page   400 402 403 404  /404.html;
            }
        }
        server {
            listen       80;
            server_name  www.rpw1.com;
            location / {
                root   /opt/web_nginx/rpw1;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  www.rpw2.com;
            location / {
                root   /opt/web_nginx/rpw2;
                index  index.html index.htm;
            }
        }
    
    
        # 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;
        #    }
        #}
    
    }

    3. 配置多个域名的虚拟主机,可以在nginx.conf配置文件中增加server{}代码块,每个域名对应一个server{}代码块.

    4. 修改配置文件nginx.conf之后,记得重启nginx服务

    [root@master conf]# ../sbin/nginx -s reload

    nginx错误页面优化

    优化404等错误页面,在server{}代码块中的error_page处修改配置.

    server {
            listen       80;
            server_name  www.rpw0.com;
    
            location / {
                proxy_pass http://rpw_upstream;
    
                #allow 10.0.3.47;
                #allow 10.0.3.158;
                #allow 10.0.3.126;
                #deny 10.0.3.47;
                
                root   /opt/web_nginx/rpw0;
                index  index.html index.htm;
            
            location /status {
                stub_status on;
            }
            error_page   400 402 403 404  /404.html;
            }
        }

    nginx限制网站IP

    • allow 允许IP访问
    • deny 拒绝IP访问
    server {
            listen       80;
            server_name  www.rpw0.com;
    
            location / {
                proxy_pass http://rpw_upstream;
    
                allow 10.0.3.47;
                allow 10.0.3.158;
                allow 10.0.3.126;
                deny 10.0.3.47;
                
                root   /opt/web_nginx/rpw0;
                index  index.html index.htm;
            
            location /status {
                stub_status on;
            }
            error_page   400 402 403 404  /404.html;
            }
        }

    nginx代理

    1. 正向代理和反向代理

    2. 实现一个反向代理

    如 10.0.3.158 代理 10.0.3.161,在 10.0.3.158上配置nginx.conf文件

    server {
            listen       80;
            server_name  www.rpw0.com;
            location / {
                proxy_pass http://10.0.3.161; #只要是请求10.0.3.158的用户,都会被转发给10.0.3.161服务器
                root   html;
                index  index.html index.htm;
            }
        }

    nginx实现负载均衡

    1. 三台服务器,一台作为nginx代理服务器(负载均衡调度器),另两台是web服务器

    10.0.3.156 # 负载均衡调度器
    10.0.3.126 # web服务器1
    10.0.3.161 # web服务器2

    2. 在负载均衡调度器上配置

    # http{}代码块中添加
    
    upstream rpw_upstream {
            server 10.0.3.126 weight=10; # weight 权重
            server 10.0.3.161 weight=5;
        }
    
    # server{}代码块中添加
    location / {
                proxy_pass http://rpw_upstream;
                root   html;
                index  index.html index.htm;
            }

    3. 三台服务器都平滑重启

    ./nginx -s reload

    4. 配置权重

    调度算法      概述
    轮询        按时间顺序逐一分配到不同的后端服务器(默认)
    weight       加权轮询,weight值越大,分配到的访问几率越高
    ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
    url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
    least_conn    最少链接数,那个机器链接数少就分发

    5. 访问10.0.3.156之后,web1服务器和web2服务器交替返回数据

    风吹散的仅仅是回忆
  • 相关阅读:
    WPF ComboBox 控件用法
    WPF 调试办法
    wpf Border 装饰控件用法
    创建型模式:抽象工厂(Abstract Factory)
    创建型模式:工厂方法
    使用FreeSpire.Doc不安装Office组件的情况下操作Word文件
    【算法一】布隆过滤器算法学习附维基百科详细说明PDF文件
    C# 读写锁解决多线程下并发写入文件报异常“文件正在由另一进程使用,因此该进程无法访问此文件”的解决办法
    多线程下System.Threading.Interlocked用法
    WPF程序内嵌CEF框架浏览网站
  • 原文地址:https://www.cnblogs.com/weige007/p/12109534.html
Copyright © 2020-2023  润新知