nodejs配置nginx 以后鏈接mongodb數據庫


服務器 :windows server2008 R2  

反向代理 :nginx 1.15.1 for window 64位

數據庫:mongodb 4 64位

使用框架express 

首先下載nodejs 在官網或者中文網下載都可以 https://nodejs.org/zh-cn/

然后將寫好的項目打包成zip 上傳 一定要帶上 package.json

這樣做可以使用npm install 直接將所有的資源全部加載 。以防出現module 找不到;

注意一點:下載mongodb時 在最后下載時 一定要去掉 可視化管理工具下載

不然你會等很久 如果網不好可能就卡死了。 這時候 需要你關閉下載的進程 

如果找不到只能重新啟動服務器。

如果沒有看懂。直接百度mongodb下載卡死 就好了。

npm是node自帶的包管理器。

將node部署到服務器上運行時 。我們需要在

因為我們配置nginx時 ,是需要修改localhost的 所有我們需要寫上'127.0.0.1';

在操作數據時,我們也應該修改成

 

否則會導致數據庫鏈接失敗。

接下來我們配置nginx服務

首先下載nginx http://nginx.org/en/download.html 下載最新版本 注意是window版

 

 然后安裝nginx。將zip包進行解壓 切記不要用鼠標直接去運行nginx.exe。

我在這里需要打開cmd進入改目錄下 然后 輸入 start nginx或者 start nginx.exe 去運行。

只運行一次就好。如果多次運行會開啟多個服務,這是一個大坑。

多開了就任務管理器關掉就好了。

不出意外的話。應該是這樣的畫面

他和node不一樣 運行沒有報錯就說明已經開啟了 但是還是會切回命令行模式。這是正常的。

你如果為了確認是否開啟nginx 你可以打開任務管理器在進程里查看 

有2個nginx就是正常啟動了 ,這時候你可以訪問localhost:8080 

出現了welcome to nginx 說明啟動運行成功。

反向代理配置:nginx.conf文件  這里配置的是 http服務 https 只需要改成 443 端口 就可了 

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

upstream nodejs {
        server 127.0.0.1:3000;  #你配置的端口
        keepalive 64;
    }



server {
    
    listen       80;
    server_name  www.xiaobaiblog.cn; #你需要解析的域名 
     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_pass      http://nodejs;

        }

   

    #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;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

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


# HTTPS server
#
#server {
#    listen       443 ssl;  
#    server_name  localhost; #你的域名 

#    ssl_certificate      cert.pem; 
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

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

 

在雲解析 將域名設置指向你的公網ip就可以了

此時你的網站應該是可以訪問了  。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM