一、下載nginx包
下載地址:http://nginx.org/download/
二、檢查環(huán)境并安裝
- gcc安裝
安裝 nginx 需要先將官網(wǎng)下載的源碼進(jìn)行編譯,編譯依賴 gcc 環(huán)境,如果沒有 gcc 環(huán)境,則需要安裝
yum install gcc-c++ - 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 - zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對(duì) http 包的內(nèi)容進(jìn)行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel - OpenSSL 安裝
OpenSSL 是一個(gè)強(qiáng)大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測試或其它目的使用。
nginx 不僅支持 http 協(xié)議,還支持 https(即在ssl協(xié)議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel - 解壓安裝
上傳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
- 配置環(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
- 配置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ā)的端口
}
}