Nginx 安装 配置 使用

nginx安装:

>本文采用编译安装的方式
  1. 解决依赖关系 
    编译安装nginx需要事先需要安装开发包组”Development Tools”和 “Development Libraries”。同时,还需要专门安装pcre-devel包:

     yum -y install zlib zlib-devel openssl openssl-devel pcre-devel gd-devel
  2. 添加用户

    groupadd -r nginx
    useradd -r -g nginx nginx
  3. 编译安装

    ./configure \
    --prefix=/usr \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-ipv6 \
    --with-http_v2_module \
    --with-http_image_filter_module \
    --with-pcre \
    --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
    make
    make install
  4. 配置支持Centos7 systemctl服务管理

    vim /lib/systemd/system/nginx.service

    添加下面内容

    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target [Service]
    Type=forking
    PIDFile=/var/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true [Install]
    WantedBy=multi-user.target

    可以通过systemctl status start stop来看nginx服务内容

  5. 配置nginx

    vim /etc/nginx/nginx.conf
    
    #在http节点下,添加upstream节点
    
    upstream mysite {
    server 172.20.13.229:8081;
    server 172.20.13.230:8082;
    } #修改server节点下的location节点 location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
    proxy_pass http://mysite;
    } #主要内容在mysite中

完成:)

05-01 07:57