server {
listen 80 default_server;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index /;
proxy_pass http://localhost:3001;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
location ~ .*\.(git|jpg|jpeg|png|bmp|swf)$ {
root /usr/local/nodeapp/public;
expires 30d;
}
location ~ .*\.(js|css)?$
{
root /usr/local/nodeapp/public;
expires 1h;
}
上述配置文件修改了/etc/nginx/conf.d/default.conf
配置环境:centos 6.5
注意一点:
第一个location:
location / {
index /;
proxy_pass http://localhost:3001;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
index / 需要配置,不然会跳转到nginx自己的默认主页
(注意:这里的/ ,因为我的nodejs主页是/ ,如果你的主页url是/index或者/login那么需要配置为 index /index或者/login)
第二个location 是nginx对静态图片文件的请求
第三个location是对css和js文件的请求
最后重启一下nginx:
/etc/init.d/nginx reload