这几天折腾了下关于在Ubuntu服务器上安装nginx,并且将个人的两个域名分别指向同一台服务器,并且支持SSL的过程。
安装Nginx:
关于nginx的安装,网上有很多,我个人的总结如下:
1.gcc --version
if not:
apt-get install build-essential
apt-get install libtool
2.安装pre依赖库
apt-get install libpcre3 libpcre3-dev
3.安装zlib
apt-get install zlib1g-dev
4.安装OpenSSL
apt-get install openssl 不过默认都会有,此处需要安装另外一个库
apt-get install libssl-dev
准备条件ok,安装nginx:
1.wget http://nginx.org/en/download.html 选择一个稳定版本
2.tar -zxvf nginx...tar.gz
3. cd nginx-dir
4 ./configure --prefix=/develop/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module 指定ssl模块
5 make && make install(默认安装目录在/usr/local/nginx)
然后执行下面操作 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Done!完成nginx的安装!
此时的nginx的启动,停止,都要切换到nginx的安装目录,执行命令才可以:
./nginx #启动
./nginx -s stop #停止
./nginx -s reload #重启
把nginx注册为Ubuntu服务:
下载脚本:(官方给出的脚本)https://amourling.cn/nginx.sh
cp 下载后的nginx.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d nginx defaults
服务注册OK!
#HTTPS SERVER
upstream abc.xyz {
server localhost:7070;
}
server {
listen 443;
server_name abc.xyz www.abc.xyz;
ssl on;
root html;
index index.html index.htm;
ssl_certificate mocki-cert/20170226.pem; #路径在/usr/local/nginx/conf/下
ssl_certificate_key mocki-cert/20170226.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://abc.xyz;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
upstream www.xxx.cn {
server localhost:8080;
}
server {
listen 443;
server_name xxx.cn www.xxx.cn;
ssl on;
root html;
index index.html index.htm;
ssl_certificate xxx-cert/20170227.pem;
ssl_certificate_key xxx-cert/20170227.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://www.xxx.cn;
}
}