1. 说明

操作系统:CentOS 7.4 64位

nginx版本:1.16.1

安装日期:2019/10/01

 

2. 开始安装

2.1 安装运行库

$ yum -y install gcc gcc-c++ automake autoconf libtool make

 

2.2 安装openssl

说明:nginx SSL使用。

$ cd /usr/local/src
$ wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
$ tar -zxvf openssl-1.0.1t.tar.gz

  

2.3 安装pcre

说明:pcre是一个正则表达式库。

$ cd /usr/local/src
$ wget https://ftp.pcre.org/pub/pcre/pcre-8.38.tar.gz
$ tar -zxvf pcre-8.38.tar.gz
$ cd pcre-8.38
$ ./configure
$ make
$ make install

 

2.4 安装zlib

说明:zlib是提供数据压缩用的函式库。

$ cd /usr/local/src
$ 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

 

2.5 安装nginx

说明:此示例的nginx版本为1.16.1。

$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
$ tar -zxvf nginx-1.16.1.tar.gz
$ cd nginx-1.16.1

$ ./configure --with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.38 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.1t

$ make
$ make install

 

3. 操作nginx

3.1 启动nginx

说明:nginx默认安装路径为 /usr/local/nginx

$ cd /usr/local/nginx
$ ./sbin/nginx

 

3.2 加载配置

/usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx

 

3.3 停止nginx

$ /usr/local/nginx/sbin/nginx -s stop 

 

4. nginx配置文件

说明:包含80、443(SSL)以及反向代理。

#user  root;
worker_processes 2;

#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;
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream api {
            server localhost:7001;
    }

    server {
        listen 80;
        listen 443 ssl;
        server_name www.test.com;
        #ssl on;
        root /usr/local/nginx/html;
        index index.html index.htm;
        ssl_certificate      /usr/local/nginx/cert/test.com.pem;
        ssl_certificate_key  /usr/local/nginx/cert/test.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
        ssl_prefer_server_ciphers on;

        location / {
            root /usr/local/nginx/html/dist;
            expires 1h;
        }

        # api
        location ^~ /api/ {
            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_pass http://api/;
        }
    }
}

  

 

posted on 2019-10-01 22:00  FangMu  阅读(3170)  评论(0编辑  收藏  举报