美文网首页
linux安装nginx

linux安装nginx

作者: 东东学习室 | 来源:发表于2019-03-05 16:44 被阅读0次

    首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)

    (配置ssl证书出错)https://blog.csdn.net/weixin_38111957/article/details/81283121

    安装make:
    yum -y install gcc automake autoconf libtool make

    安装g++:
    yum install gcc gcc-c++

    安装PCRE库:
    cd /usr/local/src
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
    tar -zxvf pcre-8.39.tar.gz
    cd pcre-8.39
    ./configure
    make && make install

    安装zlib库:
    wget http://zlib.net/zlib-1.2.11.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
    cd zlib-1.2.11
    ./configure
    make && make install

    安装openssl(某些vps默认没装ssl):
    查看是否安装openssl:
    openssl version
    如果已安装就跳过
    wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
    ./configure
    make && make install

    安装nginx:
    wget http://nginx.org/download/nginx-1.15.9.tar.gz
    ./configure
    make && make install

    配置

    在/etc/init.d/目录下,创建nginx启动脚本,并写入内容:

    #!/bin/bash
    # chkconfig: - 30 21
    # description: http service.
    # Source Function Library
    . /etc/init.d/functions
    # Nginx Settings
    NGINX_SBIN="/usr/local/nginx/sbin/nginx"
    NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
    NGINX_PID="/usr/local/nginx/logs/nginx.pid"
    RETVAL=0
    prog="Nginx"
    
    start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    reload() {
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
    }
    
    restart() {
        stop
        start
    }
    
    configtest() {
        $NGINX_SBIN -c $NGINX_CONF -t
        return 0
    }
    
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    reload)
    reload
    ;;
    restart)
    restart
    ;;
    configtest)
    configtest
    ;;
    *)
    echo $"Usage: $0 {start|stop|reload|restart|configtest}"
    RETVAL=1
    esac
    exit $RETVAL
    

    给nginx(启动脚本) 设定755权限:
    chmod 755 /etc/init.d/nginx
    添加 nginx服务 到服务列表:
    chkconfig --add nginx
    设定 nginx服务 开机启动:
    chkconfig nginx on
    修改配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    再http{}里面的最下面添加
    include /usr/local/nginx/vhost/*.conf;
    检测nginx配置文件是否有错
    /usr/local/nginx/sbin/nginx -t
    启动nginx服务
    /etc/init.d/nginx start

    在vhost里面添加admin.conf和www.conf

    erver {
        listen       80;                        
        server_name admin.alipayjf.com;    
        root  /data/www/admin;
        location / {
            try_files $uri $uri/ /index.html;
        } 
    }
    
    erver {
        listen       80;                        
        server_name www.alipayjf.com;
        location / {
            proxy_pass http://127.0.0.1:8688;
        } 
        
        location /html {
            alias /data/www/html/;
        } 
    }
    

    配置说明

    Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值。

    Server

    接收请求的服务器需要将不同的请求按规则转发到不同的后端服务器上,在 nginx 中我们可以通过构建虚拟主机(server)的概念来将这些不同的服务配置隔离。

    server {
        listen       80;
        server_name  localhost;
        root   html;
        index  index.html index.htm;
    }
    

    这里的 listen 指监听端口,server_name 用来指定IP或域名,多个域名对应统一规则可以空格分开,index 用于设定访问的默认首页地址,root 指令用于指定虚拟主机的网页跟目录,这个地方可以是相对地址也可以是绝对地址。

    Localtion

    每个 url 请求都会对应的一个服务,nginx 进行处理转发或者是本地的一个文件路径,或者是其他服务器的一个服务路径。而这个路径的匹配是通过 location 来进行的。我们可以将 server 当做对应一个域名进行的配置,而 location 是在一个域名下对更精细的路径进行配置。

    以上面的例子,可以将root和index指令放到一个location中,那么只有在匹配到这个location时才会访问root后的内容:

    location / {
            root   /data/www/host2;
            index  index.html index.htm;
    }
    

    静态文件映射

    访问文件的配置主要有 root 和 aliasp's 两个指令。这两个指令的区别容易弄混:
    alias后跟的指定目录是准确的,并且末尾必须加 /。

    location /c/ {
            alias /a/;
        }
    

    如果访问站点http://location/c访问的就是/a/目录下的站点信息。

    root后跟的指定目录是上级目录,并且该上级目录下要含有和location后指定名称的同名目录才行。

    location /c/ {
            root /a/;
        }
    

    这时访问站点http://location/c访问的就是/a/c目录下的站点信息。
    如果你需要将这个目录展开,在这个location的末尾加上「autoindex on; 」就可以了

    转发

    配置起来很简单比如我要将所有的请求到转移到真正提供服务的一台机器的 8001 端口,只要这样:

    location / {
        proxy_pass 172.16.1.1:8001;
    }
    

    这样访问host时,就都被转发到 172.16.1.1的8001端口去了。

    负载均衡

    upstream myserver; {
        ip_hash;    
        server 172.16.1.1:8001;
        server 172.16.1.2:8002;
        server 172.16.1.3;
        server 172.16.1.4;
    }
    location / {
        proxy_pass http://myserver;
    }
    

    我们在 upstream 中指定了一组机器,并将这个组命名为 myserver,这样在 proxypass 中只要将请求转移到 myserver 这个 upstream 中我们就实现了在四台机器的反向代理加负载均衡。其中的 ip_hash 指明了我们均衡的方式是按照用户的 ip 地址进行分配。另外还有轮询、指定权重轮询、fair、url_hash几种调度算法

    相关文章

      网友评论

          本文标题:linux安装nginx

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