我在配置Nginx方面遇到了问题。我有一个应用程序,我想分成2个或更多部分。应用程序的每个部分都有自己的子域。部分特定文件将放在单独的目录中,而常见文件即。 js,css,images将放在共享目录common
中。目录树将是这样的:
public/
|- app1_files/
|- app2_files/
\- common/
|- js/
|- css/
\- images/
vhost配置中的文档根目录将位于public
目录中。文件位于app1_files
的子应用程序将具有虚拟子域application1.domain.tld
,而application2.domain.tld
分别提供来自app2_files
的文件。棘手的部分是公共文件将直接从common
目录提供,因此application1.domain.tld/common/js/script.js
和application2.domain.tld/common/js/script.js
将返回相同的文件,而application1.domain.tld/js/script.js
和application2.domain.tld/js/script.js
将返回子应用程序特定文件。
这是我试图制作的配置文件,但它没有正确地重写它。它始终回退到public/index.html
或返回404。
server {
listen 80 default deferred;
# "localhost" to accept all connections, or "www.example.com"
# to handle the requests for "example.com" (and www.example.com)
server_name localhost;
# Redirect based on the Host: header sent by the browser, rather than
# the server_name variable above.
server_name_in_redirect off;
# Path for static files and default index file
root /var/www/chimera/public;
index index.html;
# We only use utf-8
charset utf-8;
# Default expiry of one month. We only serve static files
expires 1M;
# Enable SSI - we use it in our deploy scripts to minify css / js
ssi on;
# Opt-in to the future
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
# Drops obviously bad / malformed requests before we go anywhere else
include drop.conf;
# Cache .appcache <http://appcachefacts.info/> and our app's html
location ~* \.(?:manifest|appcache|html|xml|json)$ {
expires -1;
access_log /var/log/nginx/static.log;
}
# Don't need to log accesses to these files
location ~* (?:favicon\.ico|robots\.txt)$ {
expires 1w;
access_log off;
add_header Cache-Control "public";
}
# Favicon. No-one cares about hits to this file
location ~* \.ico$ {
expires 1w;
access_log off;
add_header Cache-Control "public";
}
# Media: images, video, audio, HTC, WebFonts
location ~* \.(?:jpg|jpeg|gif|png|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
expires 1M;
# Turn access_log off when ready for production
access_log /var/log/nginx/media.log;
add_header Cache-Control "public";
}
# CSS and Javascript (we use cachebusting filenames when necessary)
location ~* \.(?:css|js)$ {
expires 1M;
# Turn access_log off when ready for production
access_log /var/log/nginx/css_js.log;
add_header Cache-Control "public";
}
# All other requests
location / {
# Backbone.js uses the URL visible in the browser, we remove trailing
# slashes via a redirect so we don't have to have 2 backbone routes
rewrite ^/(.*)/$ /$1 permanent;
# Sends 404 requests to the root so backbone.js can see the URL
try_files $uri @application /index.html;
}
# Sub applications
location @application {
if ($host ~* ^application1\.(.*))
{
set $application_root "app1_files";
}
if ($host ~* ^application2\.(.*))
{
set $application_root "app2_files";
}
rewrite ^(.*)$ $application_root$1;
}
}