我一直在忙着把一个站点从Apache迁移到Nginx,我要疯了。虚拟主机不想提供静态资源(css、js等)。我似乎不知道为什么。服务器块如下所示:
server {
listen 443 default_server ssl;
server_name dev.myproject.mydomain.net;
root /opt/dev/myproject;
index index.php;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/certs/server.pem;
access_log /var/log/nginx/vh.project.dev.access.log;
error_log /var/log/nginx/vh.project.dev.error.log;
location ~ ^/alias_name/(.*) {
alias /opt/dev/myproject/www/$1;
location ~ ^/alias_name/(.+\.php)$ {
alias /opt/dev/myprojectp/www/$1;
include /etc/nginx/conf/php;
}
}
location ~ \.php$ {
include /etc/nginx/conf/php;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}我遗漏了什么?我知道这是我对Nginx缺乏经验,但在这一点上,任何建议都会很感激。
谢谢。
更新
这似乎和我以前遇到麻烦的化名有关。如果我将文档根目录指向别名位置(/opt/dev/myprojectp/www),并尝试在没有别名的情况下呈现静态内容,它就会呈现得很好。一旦我这么多地把别名扔到URL...not里。
发布于 2012-08-17 19:56:32
好吧,所以我可能已经找到了自己的答案,通过蛮力试验和错误。这个虚拟主机服务器块似乎正确地服务于PHP和静态内容:
server {
listen 443 default_server ssl;
server_name dev.myproject.mydomain.net;
root /opt/dev/project-root;
index index.php;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/certs/server.pem;
access_log /var/log/nginx/myproject.dev.access.log;
error_log /var/log/nginx/myproject.dev.error.log;
location ~ ^/alias_name/(.+\.php)$ {
alias /opt/dev/project-root/www/$1;
include /etc/nginx/conf/php;
}
location ~ ^/alias_name/(.*) {
alias /opt/dev/project-root/www/$1;
}
location ~ \.php$ {
include /etc/nginx/conf/php;
}
}我不知道我是否会遇到问题,我也不能说我完全理解其中的区别,但是简单地删除嵌套的location块似乎已经成功了。
发布于 2012-08-17 18:48:39
出发地:http://kbeezie.com/view/nginx-configuration-examples/
# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}https://stackoverflow.com/questions/12011296
复制相似问题