安裝Nginx並為node.js設定反向 Proxy

來源:互聯網
上載者:User

標籤:als   連接埠   down   version   登入系統   size   group   val   not found   

最近看了反向 Proxy和正向 Proxy的東西,想到自己的node.js伺服器是運行在3333連接埠的,也沒有為他設定反向 Proxy,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反向 Proxy

 編輯設定檔,增加以下配置內容,修改設定檔後要重啟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錯誤。

安裝Nginx並為node.js設定反向 Proxy

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.