一、下载nginx包
下载地址:http://nginx.org/download/
二、检查环境并安装
- gcc安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install gcc-c++ - PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
yum install -y pcre pcre-devel - zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel - OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输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目录下,创建nginx安装目录
mkdir nginx - 回到/home/nginx-1.16.0目录下
- 接下来安装,使用--prefix参数指定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)vi /etc/profile
(2) 在最下边加上,然后esc,:wq保存并退出
PATH=$PATH:/usr/local/nginx/sbin
export PATH
配置立即生效
source /etc/profile
(3) nginx -v查看版本
(4)启动nginx
cd /usr/local/nginx/sbin
./nginx
- 配置ssl模块
申请好证书,放在/usr/local/nginx/下自己创建的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
补充
其他简单配置
server {
listen 80;
server_name www.xxx.com; #你的域名
rewrite ^/(.*)$ https://xxx.com:443/$1 permanent; #将所有http转到https
location / {
root /web;
index index.html index.htm;
}
location ^~ /api/ {
proxy_pass http://www.xxx.com:8081/; #转发的域名加转发的端口
}
}