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

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

如何获取Nginx服务器安装文件

  • Nginx服务器的软件版本包括 Windows版 和 Linux版俩种。官网下载地址为http://nginx.org/en/download.html。网页上提供了Nginx服务器的三种版本的下载,分别是开发版本(Development version)、稳定版本(Stable version)和过期版本(Legacy versions)。下面分别介绍页面上下载部分各链接的含义:

  • "CHANGES-x.x"链接,记录的是对应版本的功能变更日志。包括新增功能、功能的优化和功能缺陷的修复等。

  • 紧接着"CHANGES-x.x"后的"nginx-x.x.x"链接,是Nginx服务器Linux版本下的下载链接,得到的后缀名为.tar.gz。

  • "pgp"链接,记录的是提供下载的版本使用PGP加密自由软件GnuPG计算后的签名。PGP可以解释为Pretty Good Privacy,是PGP公司的加密或签名工具的套件。点击链接进入相关页面,可以查看GnuPG针对本下载版本的签名,以及执行本次计算的GunPG软件版本号。

  • "nginx/Windows-x.x.x"链接,是Nginx服务器的Windows版本下载链接,文件后缀名为.zip。

一、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的配置文件,其文件内容如下:

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

#使员工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;
    #    }
    #}

}

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 8
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现nginx配置node socket.io vue的负载均衡,需要先安装nginxnode.js以及socket.io。 以下是简单的步骤: 1. 安装nginx 使用以下命令安装nginx: ``` sudo apt-get update sudo apt-get install nginx ``` 2. 配置nginx 在 /etc/nginx/conf.d/ 目录下创建一个新的配置文件,例如 socketio.conf,将以下内容粘贴到文件中: ``` upstream socketio_backend { ip_hash; server node1:3000; server node2:3000; } server { listen 80; server_name yourdomain.com; location / { proxy_pass http://socketio_backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } ``` 这个配置文件设置了一个名为 socketio_backend 的负载均衡器,它将请求转发到两个不同的Node.js服务器,分别是 node1 和 node2,它们都运行在3000端口上。 3. 安装Node.js和socket.io 在 node1 和 node2 服务器上安装 Node.js 和 socket.io,使用以下命令: ``` sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm sudo npm install socket.io ``` 4. 在Node.js使用socket.io 在 Node.js 应用程序中使用以下代码启动 socket.io: ``` var io = require('socket.io')(3000); io.on('connection', function (socket) { console.log('a user connected'); }); ``` 这段代码启动了一个 socket.io 实例,并监听在3000端口上,当有一个新的客户端连接时,会在控制台输出“a user connected”。 在Vue.js中使用socket.io 在Vue.js应用程序中使用以下代码连接到socket.io: ``` import io from 'socket.io-client'; const socket = io('http://yourdomain.com'); ``` 这段代码连接到运行在yourdomain.com的socket.io服务器。 至此,你就可以在Vue.js应用程序中使用socket.io了,并且通过nginx实现了负载均衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值