使用Nginx + Node.js部署你的网站(转)

转自:https://www.jianshu.com/p/717f2b88d057

Nginx是一个高性能的HTTP和反向代理服务器(反向代理就是通常所说的web服务器加速,它是一种通过在繁忙的web服务器和internet之间增加一个高速的web缓冲服务器来降低实际的web服务器的负载),Nginx由俄罗斯程序员利用C语言开发,以稳定、低系统资源消耗闻名,腾讯、百度、阿里、京东、网易等均有部署使用。此外,在高连接并发的情况下,Nginx是Apache的不错替代品,其能够支持高达50000个并发连接数的响应。

一、Nginx在Linux下的安装

1、编译工具和库文件的安装

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2、prce的安装
以下假设我们安装在src文件夹中
下载:

[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

解压:

[root@bogon src]# tar zxvf pcre-8.35.tar.gz 

安装:

cd pcre-8.35
 ./configure
make && make install

3、Nginx的安装
下载:

[root@bogon src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

安装:

[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@bogon nginx-1.6.2]# make [root@bogon nginx-1.6.2]# make install 

最后进入响应的目录执行nginx可执行文件即可。

二、Nginx在Windows下的安装

Windows下只需要下载解压即可使用,下载地址http://nginx.org/en/download.html
运行nginx.exe,即可启动服务,在浏览器中打开可看到以下画面,这说明Nginx已经运行起来了。

 
 

三、Nginx的配置

要运行起自己的网站我们还需要对Nginx做一些配置,在nginx文件夹的子文件夹conf下的nginx.config文件就是Nginx的配置文件,其文件内容如下:


#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; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { 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; # } #} } 

#显然代表注释,以下是这些配置的一些说明

#使员工Nginx的用户名
#user  nobody;

#cpu数,一般设置成和服务器的cpu数一致
worker_processes  1;

#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程id #pid logs/nginx.pid; events { worker_connections 1024; } http { #设置mime类型,类型由mime.type文件定义 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指令指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设定为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime sendfile on; #tcp_nopush on; #设置超时时间 #keepalive_timeout 0; keepalive_timeout 65; #是否开启gzip压缩(网页速度优化非常有用,开启后通常可以达到70%的压缩率) #gzip on; server { #侦听端口 listen 80; #域名 server_name localhost; #编码设置 #charset koi8-r; #设定虚拟主机的访问日志 #access_log logs/host.access.log main; #默认请求 location / { #默认网站的根目录 root html; #首页索引文件的名称 index index.html index.htm; } #定义错误提示页面,你还可以在这里添加500,403等,以空格分开 #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; # } #} } 

四、Nginx + Node.js部署

在Nginx中添加一个server类,如下:

server {
        listen       80;
        #域名
        server_name  huruji3.com www.huruji3.com;

        location / {
             #node.js应用的端口
             proxy_pass http://127.0.0.1:3000;
             root blog;
       }
        #静态文件交给Nginx直接处理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } } 

当然,我们为了最大化的利用域名,我们有时需要更多的使用二级域名,以运行更多的应用,同样我们只要再添加一个类:

server {
        listen       80;
        #域名
        server_name  blog.huruji3.com;

        location / {
             #node.js应用的端口
             proxy_pass http://127.0.0.1:3001;
             root blog;
       }
        #静态文件交给Nginx直接处理 location ~ *^.+\.(css | js | txt | swf | mp4)$ { root E:\huruji\blog\wechat_v1.1\public; access_log off; expires 24h; } } 

这里说明一下,我们利用二级域名是一种充分利用的域名资源的方法,同样利用路径也可以,这和使用的服务器内部采用的映射方式有关,比如院网和工作室网站对外表现就是不同的网站,但是工作室网站的/hope只是一个路径而已,Nginx不能根据路径,可以使用二级域名使得不同应用运行在同一个一级域名下。
以下的Nginx配置,打开不同域名也就访问了不同网站:


#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; server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { 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 80; server_name huruji3.com www.huruji3.com; location / { proxy_pass http://127.0.0.1:3000; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } server { listen 80; server_name blog.huruji3.com; location / { proxy_pass http://127.0.0.1:3001; root blog; } #location ~ *^.+\.*$ { # root E:\huruji\blog\wechat_v1.1\public; # access_log off; # expires 24h; #} } # 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; # } #} } 

浏览器输入不同地址,也就访问了不同网站应用:

 
 
 
 
 


作者:忽如寄
链接:https://www.jianshu.com/p/717f2b88d057
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自www.cnblogs.com/mersn/p/10511040.html