nginx安裝配置,并配置ssl模塊

一、下載nginx包

下載地址:http://nginx.org/download/

二、檢查環(huán)境并安裝

  1. gcc安裝
    安裝 nginx 需要先將官網(wǎng)下載的源碼進(jìn)行編譯,編譯依賴 gcc 環(huán)境,如果沒有 gcc 環(huán)境,則需要安裝
    yum install gcc-c++
  2. PCRE pcre-devel 安裝
    PCRE(Perl Compatible Regular Expressions) 是一個(gè)Perl庫,包括 perl 兼容的正則表達(dá)式庫。nginx 的 http 模塊使用 pcre 來解析正則表達(dá)式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發(fā)的一個(gè)二次開發(fā)庫。nginx也需要此庫。
    yum install -y pcre pcre-devel
  3. zlib 安裝
    zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對(duì) http 包的內(nèi)容進(jìn)行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
    yum install -y zlib zlib-devel
  4. OpenSSL 安裝
    OpenSSL 是一個(gè)強(qiáng)大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測試或其它目的使用。
    nginx 不僅支持 http 協(xié)議,還支持 https(即在ssl協(xié)議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
    yum install -y openssl openssl-devel
  5. 解壓安裝
    上傳nginx壓縮包到home目錄下,
  • 解壓 tar -zxvf nginx-1.7.4.tar.gz,得到nginx-1.16.0目錄
  • 前往/usr/local目錄下,創(chuàng)建nginx安裝目錄mkdir nginx
  • 回到/home/nginx-1.16.0目錄下
  • 接下來安裝,使用--prefix參數(shù)指定nginx安裝的目錄,并且 安裝 http_ssl_module with-http_stub_status_module 模塊
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
    make
    make install
  1. 配置環(huán)境變量
    (1) vi /etc/profile
    (2) 在最下邊加上,然后esc,:wq保存并退出
PATH=$PATH:/usr/local/nginx/sbin
export PATH

配置立即生效

source /etc/profile

(3) nginx -v查看版本
(4)啟動(dòng)nginx

 cd /usr/local/nginx/sbin
 ./nginx     
  1. 配置ssl模塊
    申請(qǐng)好證書,放在/usr/local/nginx/下自己創(chuàng)建的cert目錄下
    vi nginx.conf
    ssl證書配置
 server {
        listen       443 ssl;
        server_name www.xxx.com;
        ssl_certificate      cert/xxx.pem;
        ssl_certificate_key  cert/xxx.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            root   /web;
            index  index.html index.htm;
        }
    location ^~ /api/ {
            proxy_pass http://www.xxx.com:8081/;
        } 
    }

重啟nginx使得生效
nginx -s reload

補(bǔ)充
其他簡單配置

server {
        listen       80;
        server_name  www.xxx.com; #你的域名
    rewrite ^/(.*)$ https://xxx.com:443/$1 permanent;  #將所有http轉(zhuǎn)到https
        location / {
            root   /web;
            index  index.html index.htm;
        }
        location ^~ /api/ {
            proxy_pass http://www.xxx.com:8081/;   #轉(zhuǎn)發(fā)的域名加轉(zhuǎn)發(fā)的端口
        }      

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容