node.js - 在 nginx 后面使用expressjs设置响应头

标签 node.js nginx express locomotivejs

我们有一个使用 Node js (express js) 构建的 Web 应用程序,它位于 nginx 后面。

对于特定的 API,我们希望将内容类型响应 header 设置为“text/plain”。为此, Controller 中有以下代码。

res.setHeader('Content-Type', 'text/plain'); res.send(响应);

当服务器不在 nginx 后面时,这才有效。 但是当服务器位于nginx后面时,响应头仍然是'application/json'

我的 nginx 配置如下:

#kZyguser www-data;
user root;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 20000;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;

         gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;



        client_max_body_size '10M';
 ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

       server{
             location / {
              proxy_pass_header text-plain; 
                        }
             }
}

最佳答案

您尝试过在 nginx 中使用 proxy_pass_header 吗?

proxy_pass_header 内容类型;

http://wiki.nginx.org/HttpProxyModule

关于node.js - 在 nginx 后面使用expressjs设置响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111147/

相关文章:

node.js - 如何在使用express/nodejs服务的index.html中正确包含外部css和js文件?

node.js - 为什么 Appcelerator Titanium 终端与我的操作系统终端不同?

node.js heroku - app/web.1 : Error: no template specified

javascript - 异步代码执行解释

caching - 如何设置 nginx 缓存头永不过期?

javascript - Firebase Cloud 函数更新数据库中的所有条目

javascript - 如何让循环等待node.js中的回调?

wordpress - nginx 错误 "504 Gateway Timeout"

node.js - 如何使用 X-Accel-Redirect 允许访问整个文件夹

node.js - 如何将新元素推送到子文档中“发布”>“评论”>“回复”>“喜欢”作为数组