我有一个nodejs运行在端口8000和nginx端口80在同一台服务器上。
我希望Nginx能够处理所有的请求(image,css等),并将js请求转发到端口8000上的nodejs服务器。
有没有可能做到这一点。 我已经将nginxconfiguration为反向代理,但是它将每个请求转发给nodejs,但是我希望nginx处理除js以外的所有请求。
nginx的/启用的站点- /默认/
upstream nodejs { server localhost:8000; #nodejs
}
location / { proxy_pass http://192.168.2.21:8000; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
更新尝试这个:但没有任何改变,仍然nginx转发每个请求到nodejs
server { listen 192.168.2.21:80; server_name server; access_log /var/log/server/access.log; error_log /var/log/server/error.log; root /var/www/static; location / { proxy_pass http://127.0.0.1:8000; root /var/www/jsfiles; access_log on; }
静态资产
location ~* ^(?!\/).+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ { expires max; access_log off;
}
我通常做的是独立的静态内容,并在另一个位置指令处理它。 就像是:
location ~ ^/{static|images|css,...}/ { root /var/www/static; } location / { proxy_pass http://nodejs; }