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

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

一、Nginx在Linux下的安装

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

yum -y install make zlib zlib-devel gcc-c++ libtoolopenssl 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的配置文件,其文件内容如下:

#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events {
worker_connections1024;
}


http {
include mime.types;
default_typeapplication/octet-stream;#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" "$http_x_forwarded_for"';

#access_loglogs/access.logmain;

sendfileon;#tcp_nopush on;

#keepalive_timeout0;
keepalive_timeout65;#gzipon;

server {
listen 80;
server_namelocalhost;#charset koi8-r;

#access_loglogs/host.access.logmain;

location / {
root html;
indexindex.html index.htm;
}#error_page404/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_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#denyall;
#}
}# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_namesomenamealiasanother.alias;

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


# HTTPS server
#
#server {
#listen 443 ssl;
#server_namelocalhost;

#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;

#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;

#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;

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

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

#使员工Nginx的用户名#usernobody;#cpu数,一般设置成和服务器的cpu数一致worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#进程id#pidlogs/nginx.pid;events {
worker_connections1024;
}


http { #设置mime类型,类型由mime.type文件定义
include mime.types;
default_typeapplication/octet-stream;
#设定日志格式
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" "$http_x_forwarded_for"';

#access_loglogs/access.logmain;
#sendfile指令指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设定为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
sendfileon;#tcp_nopush on;
#设置超时时间
#keepalive_timeout0;
keepalive_timeout65;#是否开启gzip压缩(网页速度优化非常有用,开启后通常可以达到70%的压缩率)
#gzipon;

server {#侦听端口
listen 80;#域名
server_namelocalhost;#编码设置
#charset koi8-r;
#设定虚拟主机的访问日志
#access_loglogs/host.access.logmain;
 
#默认请求
location / {#默认网站的根目录
root html;#首页索引文件的名称
indexindex.html index.htm;
}
#定义错误提示页面,你还可以在这里添加500,403等,以空格分开
#error_page404/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_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#denyall;
#}
}# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_namesomenamealiasanother.alias;

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


# HTTPS server
#
#server {
#listen 443 ssl;
#server_namelocalhost;

#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;

#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;

#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;

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

四、Nginx + Node.js部署

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

server {
listen 80;#域名
server_namehuruji3.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_nameblog.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配置,打开不同域名也就访问了不同网站:

#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events {
worker_connections1024;
}


http {
include mime.types;
default_typeapplication/octet-stream;#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" "$http_x_forwarded_for"';

#access_loglogs/access.logmain;

sendfileon;#tcp_nopush on;

#keepalive_timeout0;
keepalive_timeout65;

gzipon;

server {
listen 80;
server_name127.0.0.1;#charset koi8-r;

#access_loglogs/host.access.logmain;

location / {
root html;
indexindex.html index.htm;
}#error_page404/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_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#denyall;
#}
}# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 80;
server_namehuruji3.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_nameblog.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_namelocalhost;

#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;

#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;

#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;

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

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

严正声明:本站只出售html页面效果模板,不提供任何类型的网站内容数据,模板仅供学习交流使用,不得用于任何商业以及触犯国家法律法规的用途,违者需自行承担全部责任,与本站无以及模板设计作者无关,本站以及模板作者不承担任何连带责任!!!!
关爱邦 | 网站源码程序下载_免费商业源码分享! » 使用Nginx + Node.js部署你的网站

发表评论

提供最优质的资源集合

立即查看 了解详情