OpenShift搭建Nginx反向代理

官方搭建Nginx教程在这里 https://blog.openshift.com/lightweight-http-serving-using-nginx-on-openshift/ ,不过官方教程写于2012年,和现在操作方式有些不同。

创建 Cartridges

  1. https://openshift.redhat.com/app/console/applications 添加新应用,选择 Do-It-Yourself 0.1 Cartridges。
  2. 创建好 Cartridges 后,使用 putty 连接到创建好的 Cartridges 。

安装 Nginx

  1. 进入 tmp 路径, 下载 Nginx 源码。

    cd $OPENSHIFT_TMP_DIR
    wget http://nginx.org/download/nginx-1.9.9.tar.gz
    tar zxf nginx-1.9.9.tar.gz
    cd nginx-1.9.9

  2. 下载 PCRE 库。

    cd $OPENSHIFT_TMP_DIR
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.bz2
    tar jxf pcre-8.39.tar.bz2

  3. 编译并安装 Nginx。

    cd $OPENSHIFT_TMP_DIR/nginx-1.9.9
    ./configure --prefix=$OPENSHIFT_DATA_DIR --with-pcre=$OPENSHIFT_TMP_DIR/pcre-8.39
    make install

配置 Nginx

  1. 获取绑定IP和绑定端口。

    env
    在结果找到 OPENSHIFT_DIY_IPOPENSHIFT_DIY_PORT,我这里的值分别是 127.13.138.1298080

  2. 编辑 Nginx 配置文件。

    vi $OPENSHIFT_DATA_DIR/conf/nginx.conf
    修改 listen 的后面的值, 将 80 改为 $OPENSHIFT_DIY_IP:$OPENSHIFT_DIY_PORT (注:需修改为 env 命令获取到的IP和端口)

http {
    …
    server {
        listen              80;
        server_name  localhost;
        … 
    }
    …
}

修改为了

http {
    …
    server {
        listen              127.13.138.129:8080;
        server_name  localhost;
        … 
    }
…
}

到这里 Nginx 已经可以启动了。

  1. 设置反向代理。

    vi $OPENSHIFT_DATA_DIR/conf/nginx.conf
    修改 location / 后的值

    http {
        …
        server {
            listen              127.13.138.129:8080;
            server_name  localhost;
            … 
            location / {
                root   html;
                index  index.html index.htm;
            }
            ...
        }
    …
    }

修改为

    http {
        …
        server {
            listen              127.13.138.129:8080;
            server_name  localhost;
            … 
            location / {
                #root   html;
                #index  index.html index.htm;
                proxy_pass http://idea.lanyus.com:80;
                #proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
            ...
        }
    …
    }

启动 Nginx

这里如果直接运行 $OPENSHIFT_DATA_DIR/sbin/nginx 命令启动 Nginx , 会报端口占用的错误,原因是创建的 Cartridges 中默认启动了一个 Ruby脚本,占用了端口,我们需要杀死响应的进程,删除对应的文件。

ps aux | grep "ruby"

找到对应的进程,我这里是

*    395552  0.0  0.0  49628  6596 ?        S    10:27   0:00 ruby /var/lib/openshift/*/app-root/runtime/repo//diy/testrubyserver.rb 127.13.138.129 /var/lib/openshift/*/app-root/runtime/repo//diy

第二个值既PID为 395552

kill 395552

对应的进程已经被杀死,下面删除对应的文件

rm -rf ~/app-root/repo/*

运行 Nginx

$OPENSHIFT_DATA_DIR/sbin/nginx

配置 Nginx 自动启动

vi $OPENSHIFT_REPO_DIR/.openshift/action_hooks/start

修改为

#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:8080
#nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy |& /usr/bin/logshifter -tag diy &
nohup $OPENSHIFT_DATA_DIR/sbin/nginx > $OPENSHIFT_DIY_LOG_DIR/server.log 2>&1 &

使用 exit 命令退出 putty

备注

为避免 OpenShift遭到GFW彻底封锁,请误将 OpenShift 反代用于翻墙等用途, 谢谢。

标签: none

已有 2 条评论

  1. 大神,学习了

  2. toffy toffy

    OpenShift以前是墙过一段时间的,我以前的wordpress就是放在上面的。不过不知道为什么现在没有墙。

评论已关闭