一. 安装编译环境
-
由于本人linux机器已经安装过编译环境了,所以这部分不在过多赘述,其他文章有很多相关方法。
-
以下借鉴http://www.nginx.cn/install中文官网内容,版本好建议用最新的版本:(转载)
ububtu平台编译环境可以使用以下指令
apt-get install build-essentialapt-get install libtool
centos平台编译环境使用如下指令
安装make:
yum -y install gcc automake autoconf libtool make
安装g++:
yum install gcc gcc-c++
一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
可以是任何目录,本文选定的是
/usr/local/src cd /usr/local/src
2.安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gztar -zxvf pcre-8.37.tar.gzcd pcre-8.34./configuremakemake install
3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcdzlib-1.2.8./configuremakemake install
4.安装ssl(某些vps默认没装ssl)
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gztar -zxvf openssl-1.0.1t.tar.gz
二. 解压源码并安装
-
进入/usr/local/目录
在当前目录下上传nginx-1.16.1.tar.gz -
解压源码
tar -zxvf nginx.tar.gz
-
进入解压好的目录 (不建议自行创建一个名为nginx目录,后序安装时会自动生成,若自行创建并把解压后的源码放里面可能会冲突)
cd /usr/local/nginx-1.16.1
-
配置
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-http_stub_status_module
-
编译并安装
make && make install
-
进入安装完毕的目录
cd /usr/local/nginx
-
检测80端口是否被占用
netstat -ano|grep 80
-
启动Nginx
/usr/local/nginx/nginx
-
浏览器查看Nginx欢迎页
页面输入http://服务器的地址:80/niginx.cn,出现欢迎页面便成功
三. 配置Nginx
-
进入Nginx安装目录并打开配置文件(非源码目录)
vim nginx.conf
-
新增https反代理
server { listen 443; server_name xxx.xxxxxx.xxx; # 监听的域名(服务器已绑定的域名) client_max_body_size 5G; # 突破上传大文件限制 ssl on; ssl_certificate /xxx/xxxxxx.pem; # https 的 pem 文件(crt文件亦可) ssl_certificate_key /xxx/xxxxxx.key; # https 的 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 / { proxy_pass http://localhost:7300; # 本地服务器地址与端口 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto $scheme; proxy_buffering off; } } server{ listen 80; server_name xxx.xxxxxx.xxx; # 监听的域名(服务器已绑定的域名) rewrite ^(.*)$ https://$host$1 permanent; # 强制 非https 跳转到 https }
若监听域名为子域名,则需要子域名的ssl证书(可在 freessl.cn 申请免费证书)否则会提示连接不安全
-
测试配置文件
nginx -t # 命令行输出 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
-
重启 nginx
nginx -s reload