• 安装Nginx并为node.js设置反向代理


    最近看了反向代理和正向代理的东西,想到自己的node.js服务器是运行在3333端口的,也没有为他设置反向代理,node.js项目的一些静态文件是完全可以部署在Nginx上,以减少对node.js的请求。

    着手开始做:

    1、Nginx依赖gcc,pcre,zlib,openssl之类的库,通过rpm -qa | grep gcc查询,没有的话都安装上。

    2、Nginx安装

     准备工作

     a) 创建用户nginx使用的www用户。
        # groupadd  www  #添加www组    
        # useradd -g  www www -s /bin/false  #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
        创建安装目录与日志目录
        b) 安装目录
        # mkdir /usr/local/nginx
        c) 日志目录
        # mkdir /data0/logs/nginx
        # chown www:www /data0/logs/nginx -R

     安装Nginx

     下载包:wget http://nginx.org/download/nginx-1.4.2.tar.gz

     解压后进入目录

     配置编译然后安装:

     # ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

     # make

     # make install

     查看lib文件是否都链接好了

     # ldd $(which /usr/local/nginx/sbin/nginx)

     如果有not found,像libpcre.so.1 => not found,则需要手动链接一下,当时我的服务器就出现了这个问题

     # ln -s /usr/local/lib/libpcre.so.1 /lib64/ 

     检查是否安装成功

     # cd /usr/local/nginx/sbin
     # ./nginx -t

     显示下面内容表示成功了:

     nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    打开浏览器输入服务器ip发现已经进入欢迎界面,表示成功。

    3、配置node.js反向代理

     编辑配置文件,增加以下配置内容,修改配置文件后要重启Nginx:nginx -s reload

    upstream nodejs__upstream {
        server 127.0.0.1:3000;
        keepalive 64;
    }
    server {
        listen       80;
        server_name  jianqunzhang.com www.jianqunzhang.com;
        
        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        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_set_header   X-NginX-Proxy    true;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_pass         http://nodejs__upstream;
    }

     打开浏览器访问,发现直接访问服务器80端口,已经能访问原3333端口的内容了。查看响应头发现服务器已经是nginx了

    3333端口响应头

    1. Connection:
      keep-alive
    2. Content-Length:
      1474
    3. Content-Type:
      text/html; charset=utf-8
    4. Date:
      Sat, 22 Jul 2017 09:07:24 GMT
    5. ETag:
      W/"5**********************Mrw"
    6. X-Powered-By:
      Express

    80端口响应头

    1. Connection:
      keep-alive
    2. Content-Length:
      1474
    3. Content-Type:
      text/html; charset=utf-8
    4. Date:
      Sat, 22 Jul 2017 09:06:07 GMT
    5. ETag:
      W/"5c*********************UMrw"
    6. Server:
      nginx/1.9.10
    7. X-Powered-By:
      Express

    nginx默认配置是

    location / {
      root html;
      index index.html index.htm;
    }

    表示对该服务器80端口的请求直接对应 html目录下的内容

    如果想过滤静态资源直接由Nginx服务,可以直接添加下面的配置:

    location /file {
      root html;
      index index.html index.htm;
    }

    注意:要在html目录下创建对应的file文件夹,这样才能找到对应的资源,否则报404错误。

  • 相关阅读:
    android intent 隐式意图和显示意图(activity跳转)
    Stack栈的三种含义
    gdi+ 高速绘制透明窗体
    Hibernate Criterion
    iOS安全攻防(三):使用Reveal分析他人app
    Android入门第八篇之GridView(九宫图)
    【Android动画】之Tween动画 (渐变、缩放、位移、旋转)
    Ubuntu下deb包的安装方法
    理解class.forName()
    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功
  • 原文地址:https://www.cnblogs.com/miaodi/p/7222037.html
Copyright © 2020-2023  润新知