nodejs+nginx给远程服务器配置反向代理部署多个域名

在阿里云上购买了一个域名,例如mine.com,现在想在远程服务器上部署两个微信公众号项目,wechat1和wechat2,域名分别为wechat1.mine.com、wechat2.mine.com(端口均为80),分别指向mine.com:70、mine.com:90,
(因为微信公众平台服务器url以http开头的只支持80)

工具PuTTY、电脑

  1. 打开putty,输入IP地址/域名、端口号
    nodejs+nginx给远程服务器配置反向代理部署多个域名_第1张图片

  2. 输入用户名和密码进入根目录,你想把文件放在那里,就进到该路径,我文件的存放路径是根目录下的 /usr/local/src/

  3. 下载安装nginx及相关组件,命令如下

      1、openssl ——  ssl 功能需要openssl库
    

    [root@iuynskfw5dfgsff src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
    [root@iuynskfw5dfgsff src]# tar zxvf openssl-fips-2.0.10.tar.gz
    [root@iuynskfw5dfgsff src]# cd openssl-fips-2.0.10
    [root@iuynskfw5dfgsff openssl-fips-2.0.10]# ./config && make && make install

     2、pcre	——   rewrite模块需要 pcre 库
    

    [root@iuynskfw5dfgsff src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
    [root@iuynskfw5dfgsff src]# tar zxvf pcre-8.40.tar.gz
    [root@iuynskfw5dfgsff src]# cd pcre-8.40
    [root@iuynskfw5dfgsff pcre-8.40]# ./configure && make && make install

    3、zlib ——  gzip模块需要 zlib 库
    

    [root@iuynskfw5dfgsff src]# wget http://zlib.net/zlib-1.2.11.tar.gz
    [root@iuynskfw5dfgsff src]# tar zxvf zlib-1.2.11.tar.gz
    [root@iuynskfw5dfgsff src]# cd zlib-1.2.11
    [root@iuynskfw5dfgsff zlib-1.2.11]# ./configure && make && make install

    4、nginx	
    

    [root@iuynskfw5dfgsff src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
    [root@iuynskfw5dfgsff src]# tar zxvf nginx-1.10.2.tar.gz
    [root@iuynskfw5dfgsff src]# cd nginx-1.10.2
    [root@iuynskfw5dfgsff nginx-1.10.2]# ./configure && make && make install

  4. 配置
    进入/usr/local/nginx/conf,并打开nginx.conf,找到如下代码

server {
        #nginx的监听端口
        listen       80; 
        #这里是请求域名
        server_name  localhost;    改为   server_name  wechat1.mine.com;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             #-----再此新增 如下代码proxy_pass 后 可写域名/IP  --------
              proxy_pass http://mine.com:70;  #请求转发到哪里
          # root   html;
         # index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

将上面代码复制一遍粘贴到它后面,将server_name 改为 wechat2.mine.com


server {
        #nginx的监听端口
        listen       80; 
        #这里是请求域名
        server_name  localhost;    改为   server_name  wechat2.mine.com;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             #-----再此新增 如下代码proxy_pass 后 可写域名/IP  --------
              proxy_pass http://mine.com:90;  #请求转发到哪里
          # root   html;
         # index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
  1. 检查配置是否正确
    /usr/local/nginx/sbin/nginx -t
    执行后,如果正确则会输出结尾为ok和successful的两句话,如果报错则根据错误修改。

  2. 启动nginx
    首先,通过whereis nginx确定 nginx 安装到了哪里;
    然后,进入该目录;
    在这里插入图片描述
    最后通过 /usr/local/nginx/sbin/nginx 启动nginx
    如果什么都没输出则启动成功,否则会输出错误信息。
    参考文档1 参考文档2

你可能感兴趣的:(nginx)