nginx反向代理IIS一键部署https

作者: ropon 分类: Shell 发布时间: 2017-04-14 14:50
:: $Name:         win-ssl.bat
:: $Version:      v1.0
:: $Function:     nginx反向代理IIS一键部署https
:: $Author:       Ropon
:: $organization: west.cn
:: $Create Date:  2017-3-10
:: $Description:  1、nginx反向代理IIS一键部署https
::                2、支持预装环境及纯净版使用iis web环境
::                3、部署后nginx配置文件推荐放到d:/nginx/conf/vhost/目录下
::                4、证书路径d:/nginx/ssl 以域名命名www.test.com.crt www.test.com.key
::                5、部署后nginx站点配置文件名为www.test.com.conf
::
@echo off&setlocal enabledelayedexpansion
set base=http://download.myhostadmin.net/php/ioncube
if not exist %cd%\wget.exe (
	echo.
	echo 缺少wget.exe程序
	explorer.exe http://download.myhostadmin.net/wget.exe
	echo 正在下载wget.exe必要程序,请保存到当前目录下
	echo 下载完成后按任意键继续
	pause
)
if not exist %cd%\wget.exe (
	echo.
	echo 自动头下载失败请访问 http://download.myhostadmin.net/wget.exe 手动下载
	echo 并保存到当前目录
	echo.
	pause
	exit
)
echo.
if not exist %cd%\sed.rar (
	call:download http://download.myhostadmin.net/memcache/sed.rar %cd%\sed.rar
	"C:\Program Files\WinRAR\rar.exe" x -inul -o+  %cd%\sed.rar   %cd%  -y
	cls
)

goto menu

:menu
echo.
echo Nginx反向代理IIS,部署多个https站点
echo.
set path=D:\nginx\
set sslpath=D:\ssl
C:\Windows\system32\cacls D:\ssl /e /g nginx:f /T
if not exist %sslpath% (
	md %sslpath%
)	
call:existfloder %path%

echo 请输入域名: 
echo.比如:www.test.com

set /p domain=

set "URL=http://ip.chinaz.com/getip.aspx"  
for /f "tokens=2 delims='" %%a in ('wget -q "%URL%" -O -') do (  
    set "PublicIP=%%a"  
) 

set b=listen "%PublicIP%":443;

echo 是否我司申请证书,若是需要先合并: 
echo.
echo.
echo   1  是
echo   2 不是
echo.

set /p crtchoice=
if %crtchoice% ==1 goto crt11
if %crtchoice% ==2 goto crt22

:crt11

echo 请输入crt1证书路径: 
echo.比如:d:\www.test.com.cer

:crta
set /p crt1=
call:existssl %crt1% crta
C:\Windows\system32\more "%crt1%" > "%cd%\%domain%.crt"
set crttemp=%crt1:~0,-4%
set crt2temp=%crttemp%_ca.crt
set keytemp=%crttemp%.key
if exist %crt2temp% ( C:\Windows\system32\more "%crt2temp%" >> "%cd%\%domain%.crt" ) else ( echo 请输入crt2证书路径: 
	echo.比如:d:\www.test.com_ca.crt
	echo.
	:crtb
	set /p crt2=
	call:existssl %crt2% crtb
	C:\Windows\system32\more "%crt2%" >> "%cd%\%domain%.crt"
)
copy "%cd%\%domain%.crt" "%sslpath%\%domain%.crt" >nul 2>nul
set crt="d:\\ssl\\%domain%.crt"

goto key11

:crt22

echo 请输入crt证书路径: 
echo.比如:d:\www.test.com.crt
:crtc
set /p crt3=
call:existssl %crt3% crtc

copy "%crt3%" "%sslpath%\%domain%.crt" >nul 2>nul
set crt="d:\\ssl\\%domain%.crt"

goto key11

:key11
if exist %keytemp% ( copy "%keytemp%" "%sslpath%\%domain%.key" >nul 2>nul ) else ( echo 请输入key证书路径: 
echo.比如:d:\www.test.com.key
:keya
	set /p key1=
	call:existssl %key1% keya
	copy "%key1%" "%sslpath%\%domain%.key" >nul 2>nul
)

set key="d:\\ssl\\%domain%.key"

copy "%path%conf\temp.conf" "%path%conf\vhost\%domain%.conf" >nul 2>nul

%cd%\sed\sed.exe -i "/ssl/a\	%b%" %path%conf\vhost\%domain%.conf
%cd%\sed\sed.exe -i "/listen/a\	server_name %domain%;" %path%conf\vhost\%domain%.conf
%cd%\sed\sed.exe -i "/server_name/a\	ssl_certificate %crt%;" %path%conf\vhost\%domain%.conf
%cd%\sed\sed.exe -i "/ssl_certificate/a\	ssl_certificate_key %key%;" %path%conf\vhost\%domain%.conf
%cd%\sed\sed.exe -i "/listen 80;/a\	server_name %domain%;" %path%conf\vhost\%domain%.conf

echo.
echo 正在重载nginx服务……
echo.
C:\Windows\system32\net stop nginxd
C:\Windows\system32\net start nginxd
echo 安装完成,请关闭窗口
echo.
echo.
echo   1 继续部署其他域名
echo   2 清理下载文件并提出
echo   0 退出
echo.
set /p choice=
if %choice% ==1 goto menu
if %choice% ==2 goto del
if %choice% ==0 goto exit

:download
echo.
echo 正在下载所需组件
echo.
%cd%\wget.exe %~1 -O %~2
goto:eof

:existssl
echo.
if not exist %~1 (
	echo.
	echo 输入证书路径不对,请重新输入
	echo.
	goto %~2
)

:existfloder
echo.
if not exist %~1 (
	call:download http://download.myhostadmin.net/nginx.rar d:\nginx.rar
	"C:\Program Files\WinRAR\rar.exe" x -inul -o+  d:\nginx.rar  d:\  -y
	echo.
	echo 核实当前服务器没有安装Nginx反向代理环境
	echo.
	echo 请手动运行d:\nginx\install.bat 初始化环境
	echo.
	pause
	exit
)
goto:eof

:del
echo.
echo 正在清理之前下载文件及文件夹……
echo.
for /f "delims=" %%i in ('dir /b /a-d /s "sed*"') do del %%i  
rd /s/q %cd%\sed
pause
exit

:exit
exit

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!