Linux安装Nginx

葛大爷 互联网 2015-09-16 1669
  1. 先安装Nginx依赖组件
    [root@VM-20-12-centos data]# yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  2. 安装nginx
    • Nginx安装包可以在http://nginx.org/下载
    • [root@VM-20-12-centos data]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
      [root@VM-20-12-centos data]# tar -zxvf nginx-1.18.0.tar.gz
      [root@VM-20-12-centos data]# rm -rf nginx-1.18.0.tar.gz [root@VM-20-12-centos data]# cd nginx-1.18.0
      [root@VM-20-12-centos nginx-1.18.0]# ./configure
      [root@VM-20-12-centos nginx-1.18.0]# make
      [root@VM-20-12-centos nginx-1.18.0]# make install
    • 为安全起见,使用专门的用户和组业启动nginx
      [root@VM-20-12-centos nginx-1.18.0]# groupadd -f nginx_group
      [root@VM-20-12-centos nginx-1.18.0]# useradd -g nginx_group nginx_worker
    • 修改nginx配置文件
      [root@VM-20-12-centos nginx-1.18.0]# vim /usr/local/nginx/conf/nginx.conf
      user nginx_worker nginx_group;

      在 #gzip  on; 后面加入下面配置 :

       #gzip  on;
      upstream backend{
      server 192.168.86.130:8080;
      server 192.168.86.131:8080;
      ip_hash;
      }

      修改server {location / { }}其中 proxy_pass 参数和 upstream backend{} 对应

               location / {
      root html;
      index index.html index.htm index.jsp;
      proxy_pass http://backend;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      client_max_body_size 10m;
      client_body_buffer_size 128k;
      proxy_connect_timeout 90;
      proxy_send_timeout 90;
      proxy_read_timeout 90;
      proxy_buffer_size 4k;
      proxy_buffers 4 32k;
      proxy_busy_buffers_size 64k;
      proxy_temp_file_write_size 64k;
      }
    • 启动、停止nginx
      [root@VM-20-12-centos nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      [root@VM-20-12-centos nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s quit