5.1nginx的安装

官方网站:http://nginx.org/

一、  安装要求的环境

1、需要安装gcc的环境。

  yum -y install gcc-c++

2、第三方的开发包。

a) PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

  yum install -y pcre pcre-devel

  注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

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

  yum install -y zlib zlib-devel

c) OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

  nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

  yum install -y openssl openssl-devel

二、安装步骤

第一步:把nginx的源码包上传到linux系统
第二步:解压缩 [root@localhost
~]# tar zxf nginx-1.8.0.tar.gz
第三步: 目录指定为
/var/temp/nginx,需要在/var下创建temp及nginx目录: mkdir /var/temp/nginx/client -p
第四步:使用configure命令创建一makeFile文件。 .
/configure \ --prefix=/usr/local/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 [root@localhost sbin]# mkdir /var/temp/nginx/client -p 第四步:编译,使用:make 第五步:make install

三、  启动nginx

1.查看安装目录

 2.进入到sbin目录

[root@localhost sbin]# ./nginx 

访问的默认端口是80端口。注意:看是否关闭防火墙。

四、  nginx3个命令

启动命令
./nginx
关闭命令 .
/nginx -s stop 和 ./nginx -s quit
刷新配置文件 .
/nginx -s reload 修改了nginx.conf文件之后,可以不重启Nginx,能马上生效

五、  nginx配置虚拟主机

1、Nginx的配置文件(nginx.conf)配置项解释如下:

  /usr/local/nginx/conf/nginx.conf

#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 {    // 一个server节点就是一个虚拟主机
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;    //Html是nginx安装目录下的html目录
            index  index.html index.htm;
        }
    }

    server {   //一个server节点就是一个虚拟主机

      listen 80;
      server_name www.xxxx.com;
        location / {

          //客户端获取ip为127.0.0.1处理方法,添加以下4个 proxy_set_header
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header REMOTE-HOST $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          client_max_body_size 2048m;
          proxy_pass http://localhost:8081;
          index index.jsp index.html index.htm;
        }
      }

}

猜你喜欢

转载自www.cnblogs.com/L237/p/12382168.html
5.1
今日推荐