2014-02-19 30 views
0

我环顾了其他例子,试图复制一个非常基本的nginx服务器布局。但是,它根本没有工作(除了example.com,它可以正确地重定向到index.html)。nginx配置文件不工作

这是我的nginx的配置文件:

server { 
    listen 80; 
    root /var/www/example.com/public_html; 

    server_name example.com; 

    location/{ 
     root /var/www/example.com/public_html/templates; 
     try_files $uri $uri/ /index.html; 
    } 
    location /draw/ { 
     root /var/www/example.com/public_html/templates/projects/draw; 
     try_files $uri $uri/ /drawsomething.html; 
    } 
    location ~ \.(gif|jpg|png)$ { 
     root /var/www/example.com/public_html/templates/pic; 
    } 
} 

基本上,我存储所有/templates/。我想这样做,当有人去example.com/draw它显示drawsomething.html,这是在/var/www/example.com/public_html/templates/projects/draw。我遵循网站上的例子,它似乎没有工作。尽管如此,图像的重定向似乎正在起作用。另外,如果我想包含脚本文件呢?我可以使用:

location ~ \.js$ { 
    root /var/www/example.com/public_html/templates/script; 
} 
+0

@ AD7six所以我只是把一个主根放在顶部,然后把每个位置块的子根? – royhowie

+0

其实我在那里犯了一个错误 - 有一个服务器根,所以它不会中断 - 它看起来有点奇怪:你知道什么根源在做什么?有urls/foo'/foo.gif','/ foo.js'去不同的地方可能会变得很混乱。 – AD7six

+0

@ AD7six基本上,现在我想'/ template'下的所有东西。像“index.html”这样的主文件将位于此目录中。在模板内部,我会有'/ pic','/ projects'和'/ script'(稍后移动脚本)。因此,'example.com/draw'应该得到'templates/projects/draw /'里面的draw.html。脚本/图像文件将大致相同,除了在各自的目录中。 – royhowie

回答

0

我能修复我的nginx配置与以下设置。我最终通过将所有流量重定向到端口15301(node.js正在侦听的端口)来使用node.js的代理系统。

http { 
    server { 
     listen 80; ## listen for ipv4; this line is default and implied 
     #listen [::]:80 default ipv6only=on; ## listen for ipv6 

     root /var/www/royhowie.com/public_html/; 
     index index.html; 

     # Make site accessible from http://localhost/ 
     server_name localhost; 

     location/{ 
      proxy_pass http://localhost:15301/; 
      proxy_set_header Host $host; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     } 
     location /doc/ { 
      alias /usr/share/doc/; 
      autoindex on; 
      allow 127.0.0.1; 
      deny all; 
     } 
    } 
}