nodejs與nginx的完美搭配
阿新 • • 發佈:2019-01-03
引言
node自己本身可以作為伺服器進行驅動,但是node本身對檔案的處理能力並不是很好,所以當我們的生產環境中應儘量使用nginx來處理靜態的資源以及反向代理,同時也解決了node分散式以及負載均衡的相關問題。
nginx的安裝以及配置
這裡以cenos環境為基礎進行配置
1、基礎編譯環境的配置C/C++等編譯工具以及工具庫:
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、PRCE安裝
(1)下載:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
(2)解壓
tar -zxvf pcre-8.35.tar.gz
(3)安裝
cd pcre-8.35
./configure
make && make install
3、安裝Nginx
(1)下載
wget http://nginx.org/download/nginx-1.10.3.tar.gz
(2)解壓、安裝
tar -zxvf nginx-1.10.3.tar.gz cd /nginx-1.10.3 ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 make && make install
(3)是否安裝成功
/usr/local/webserver/nginx/sbin/nginx -v
能夠檢視版本號說明安裝成功
(4)配置
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
(5)實現node反向代理
node預設埠號為3000
編輯 nginx.conf 檔案
#user nobody; worker_processes 1; ##預設的CPU核心數 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { #進行如下配置 use epoll; worker_connections 65535; } 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; #主機配置 server { listen 80; server_name localhost; #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_pass http://127.0.0.1:3000; proxy_redirect default; # 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; #} } # 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; # } #}
}
檢視是否配置成功:
/usr/local/webserver/nginx/sbin/nginx -t
列印如下資訊代表成功:
nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
(6)啟動
/usr/local/webserver/nginx/sbin/nginx