upstream lunaserver {
server localhost:3000;
server localhost:3001;
}
server {
listen 80;
server_name localhost;
root /www/luna-server/public;
# individual nginx logs for this lunas-erver vhost
access_log /var/lib/luna/log/luna-server_access.log;
error_log /var/lib/luna/log/luna-server_error.log;
location / {
try_files $uri @luna-server;
}
location @luna-server {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
client_max_body_size 360M;
client_body_buffer_size 5256K;
proxy_pass http://lunaserver;
}
#for monitorix
location /monitorix {
index index.html;
}
location ~ \.pl|cgi$ {
# try_files $uri =404;
gzip off;
fastcgi_pass 127.0.0.1:8999;
fastcgi_index index.pl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Originating-IP $remote_addr;
proxy_set_header HTTP_REMOTE_ADDR $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
}
#for freeswtich port forwarding
location /webapi {
proxy_pass http://127.0.0.1:8180;
proxy_redirect 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;
proxy_set_header Authorization "Basic ZnJlZXN3aXRjaDp3b3Jrcw==";
}
#for lunahoge port forwarding
location /mediafiles {
proxy_pass http://127.0.0.1:9090/;
proxy_redirect 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 识别出各种静态文件,例如 images 路径下的所有请求可以写为:
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
location ~ ^/uploads/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires
指令来控制其在浏览器的缓存,以减少不必要的请求。 expires
指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires: