Symfony Api,Nginx,Dotcloud,add_headers

时间:2012-08-23 22:43:27

标签: nginx symfony-1.4 cors dotcloud

我在dotcloud沙箱上运行了Symfony 1.4。

我已经对nginx.conf进行了一些修改,以使symfony工作。在这个Symfony安装中,我有3个应用程序:Frontend,Backend和Api。

我只需要为Api为Cors添加标题。

这些是标题

add_header Access-Control-Allow-Header 'origin, x-requested-with, content-type';
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
add_header Access-Control-Allow-Origin '*';

如果我将此标题放在文件的根目录中,它们会起作用,但会将它们发送到我的所有应用程序。

如果我把它们放在这个块里面

location ^~ /api.php/ {
  add_header Access-Control-Allow-Header 'origin, x-requested-with, content-type';
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
  add_header Access-Control-Allow-Origin '*';
  try_files $uri /api.php?$query_string;
}

它没有用。

有什么建议吗? 提前谢谢。

编辑

我添加了运行的nginx.conf文件的全部内容

# If you want to active expires header, that's the place
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
  expires 7d;
  break;
}

  add_header Access-Control-Allow-Header 'origin, x-requested-with, content-type';
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
  add_header Access-Control-Allow-Origin '*';

# Production handler
location / {
  try_files $uri $uri/ /index.php;
}

# Dev handler - do not let this on production server
location ^~ /frontend_dev.php/ {
  try_files $uri /frontend_dev.php;
}
location ^~ /backend_dev.php/ {
  try_files $uri /backend_dev.php;
}
location ^~ /backend.php/ {
  try_files $uri /backend.php;
}

location ^~ /api_dev.php/ {
  try_files $uri /api_dev.php?$query_string;
}
location ^~ /api.php/ {
  try_files $uri /api.php?$query_string;
}


# Note: if you have others applications and handlers, used this samples above

# If don't have a symbolic link to symfony/data/web/sf/,
# you can make an alias (pay atention to your symfony directory)
location ^~ /sf/ {
  alias /home/dotcloud/code/lib/vendor/symfony/data/web/sf/;
}

这是我希望它的方式,但它不起作用

# If you want to active expires header, that's the place
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
  expires 7d;
  break;
}

# Production handler
location / {
  try_files $uri $uri/ /index.php;
}

# Dev handler - do not let this on production server
location ^~ /frontend_dev.php/ {
  try_files $uri /frontend_dev.php;
}
location ^~ /backend_dev.php/ {
  try_files $uri /backend_dev.php;
}
location ^~ /backend.php/ {
  try_files $uri /backend.php;
}

location ^~ /api_dev.php/ {
  add_header Access-Control-Allow-Header 'origin, x-requested-with, content-type';
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
  add_header Access-Control-Allow-Origin '*';
  try_files $uri /api_dev.php?$query_string;
}
location ^~ /api.php/ {
  add_header Access-Control-Allow-Header 'origin, x-requested-with, content-type';
  add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
  add_header Access-Control-Allow-Origin '*';
  try_files $uri /api.php?$query_string;
}


# Note: if you have others applications and handlers, used this samples above

# If don't have a symbolic link to symfony/data/web/sf/,
# you can make an alias (pay atention to your symfony directory)
location ^~ /sf/ {
  alias /home/dotcloud/code/lib/vendor/symfony/data/web/sf/;
}

0 个答案:

没有答案