Linux nginx安裝

1. 說明

操做系統:CentOS 7.4 64位javascript

nginx版本:1.16.1html

安裝日期:2019/10/01java

 

2. 開始安裝

2.1 安裝運行庫

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

 

2.2 安裝openssl

說明:nginx SSL使用。nginx

$ 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是一個正則表達式庫。c++

$ 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是提供數據壓縮用的函式庫。web

$ 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 api

$ 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)以及反向代理。session

#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/;
        }
    }
}

  

 

相關文章
相關標籤/搜索