文章标题 nginx 实现express 高并发接口

来源:互联网 发布:民用枪持枪证 知乎 编辑:程序博客网 时间:2024/06/03 18:31

之前用ssh写过一个后端服务接口,通过一个api向外部提供天气预报数据,但是并发量一旦大了,反应时长就会变的很长,也容易爆内存不足的问题。后来了解到node.js 处理高并发的优势,就拿java+tomcat 和node.js 做了实验对比。
整体框架结构为,前端nginx 做反向代理,转向两个http server,httpserver 从缓存中去读取数据,并立马返回,服务端脚本定时去更新缓存中的数据,为确保每一个客户端访问的数据的一致性,nginx配置如下。
nginx.conf

#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 weather.deyuan.com {     ip_hash;    server 192.168.6.120:3000;     server 192.168.6.80:3000;     }    server {        listen       83;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {           proxy_pass http://weather.deyuan.com;            proxy_set_header Host $host;            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }        #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;    #    }    #}}

express 路由配置:

var express = require('express');var redis = require('redis');var util=require("util")var client = redis.createClient(6379,'127.0.0.1');var router = express.Router();/** * 本级目录对应上一级目录为WeatherApi * 为天气预报相关接口 *//* 天气预报数据接口 参数cityName 为城市名称      */router.get('/getData', function(req, res, next) {    cityName = req.query.cityName;    client.get(cityName, function (err, reply) {        res.send(reply);    });})/* 天气预报Token接口   */router.get('/getToken', function (req, res,next) {    var str=util.format('%s=%s&%s=%s', 'host_lbs', 'v.juhe.cn', 'key_lbs','9c6b8aaa4a399952faad5765313849de');    res.send(str);})module.exports = router;