云服务器centeros7,nginx反向代理+ssh实现内网穿透

一、什么是内网穿透

             内网穿透的原理可见这里,http://service.oray.com/question/5571.html
            然后,更简单的、更贴切生活的说法就是,使用ip地址或域名通过公网访问服务器服务器反向代理转发到指向到本地的服务器,这样就可以实现通过公网ip或域名访问本地的应用了

       

云服务器centeros7,nginx反向代理+ssh实现内网穿透

二、实现步骤

1.准备好一台公网服务器,(我使用的是阿里云服务器centeros7.3)

2.最好有一个域名,这样方便一点,没有域名也行。

1.登录云服务器。

云服务器centeros7,nginx反向代理+ssh实现内网穿透

2.安装epel仓库

yum install epel-release -y

3.安装nginx

yum install nginx -y

4.启动nginx

nginx  即可或   systemctl start nginx (后面介绍启动centeros6.9的nginx)

5.配置nginx代理服务

cd /etc/nginx/conf.d          文件路径下。

vi xxxx.xxx.conf       xxxx.xxx是你的域名,如  fx.foxfly.com.conf (vi编辑器打开这个文件,如果没有回自动创建)

添加配置:

upstream tunnel {
  server 127.0.0.1:7078;
}

server {
  listen 80;
  server_name fx.foxfly.com;          # 这里填你的域名,如果没有域名则填localhost
  
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    
    proxy_pass http://tunnel;
  }
}

云服务器centeros7,nginx反向代理+ssh实现内网穿透

  
  然后重启nginx,  nginx -s reload   或者  service nginx reload

然后去服务器开放你的配置的端口,如阿里云 去配置安全组规则---》实例安全组>内网方向全部规则。添加你配置的端口

6.搭建SSH通道

我在windows上使用的是OpenSSH,安装以后,就可以在windows是使用SSH命令将服务器请求转发本地了

ssh -vnNt -R 7078:localhost:[email protected]

7078是你云服务器的端口, 3434是你本地服务器的端口,后面ip是你的服务器的公网ip。

7.需要配置HTTPS按照如下配置即可

(1)申请免费SSL证书
https://help.aliyun.com/document_detail
(2)下载证书
下载完成后,得到 .key, .pem两个文件
(3)创建证书
cd /etc/nginx
mkdir ssl
cd ssl
vi fx.foxfly.vip.key
将.key的内容复制粘贴,保存退出
vi fx.foxfly.vip.pem
将.pem的内容复制粘贴,保存退出
(4)配置 nginx
复制:
ssl on;
ssl_certificate   ssl/ fx.foxfly.vip.pem;
ssl_certificate_key  ssl/ fx.foxfly.vip.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;
到:cd ../   vi conf.d/ fx.foxfly.vip.conf
同时修改端口号为 443

将以上我的域名修改为你的域名就可以了。

最后说一下center6.9启动nginx的配置:

1)启动脚本
#!/bin/sh  
# chkconfig: 2345 85 15  
# description:Nginx Server  
  
NGINX_HOME=/usr/local/nginx  
NGINX_SBIN=$NGINX_HOME/sbin/nginx  
NGINX_CONF=$NGINX_HOME/conf/nginx.conf  
NGINX_PID=$NGINX_HOME/logs/nginx.pid  
  
NGINX_NAME="Nginx"  
  
. /etc/rc.d/init.d/functions  
  
if [ ! -f $NGINX_SBIN ]  
then  
    echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "  
    exit  
fi  
  
start() {  
    $NGINX_SBIN -c $NGINX_CONF  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Starting $NGINX_NAME: " /bin/true  
    else  
        action $"Starting $NGINX_NAME: " /bin/false  
    fi  
}  
  
stop() {  
    kill `cat $NGINX_PID`  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Stopping $NGINX_NAME: " /bin/true  
    else  
        action $"Stopping $NGINX_NAME: " /bin/false  
    fi  
}  
  
restart() {  
    stop  
    start  
}  
  
check() {  
    $NGINX_SBIN -c $NGINX_CONF -t  
}  
  
  
reload() {  
    kill -HUP `cat $NGINX_PID` && echo "reload success!"  
}  
  
relog() {  
    kill -USR1 `cat $NGINX_PID` && echo "relog success!"  
}  
  
case "$1" in  
    start)  
        start  
        ;;  
    stop)  
        stop  
        ;;  
    restart)  
        restart  
        ;;  
    check|chk)  
        check  
        ;;  
    status)  
        status -p $NGINX_PID  
        ;;  
    reload)  
        reload  
        ;;  
    relog)  
        relog  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"  
        exit 1  
esac
(2)启动
service nginx start / stop
chkconfig nginx on
注:启动报错修复
vim /etc/nginx/conf.d/default.conf

listen       80 default_server;
listen       [::]:80 default_server;
改为:
listen       80;
#listen       [::]:80 default_server;