2013-12-21 5 views
5

현재 nginx 구성에 문제가 있습니다. 내가 뭘하려고 수 있습니다 :nginx + nodejs 구성

어떤 경로없이 요청을
  • , 얻을 index.html을 (작품)
  • 직접 파일을 기존 받기 (작품)
  • 요청 된 파일이나 경로가 물리적으로하지 않는 경우 존재, 프록시 요청을 nodejs (404)

내가 stackoverflow에 여러 가지 구성을 시도했지만, 그들 중 누구도 내 요구에 적합하지 않습니다. 나는 당신이 뭘하려고했는지 알아 냈다고 생각

# IP which nodejs is running on 
upstream app_x { 
    server 127.0.0.1:3000; 
} 

# nginx server instance 
server { 
listen 80; 
server_name x.x.x.x; 
#access_log /var/log/nginx/x.log; 

root /var/www/x/public; 

location/{ 
    root /var/www/x/public; 
    index index.html index.htm index.php; 
} 

location ^/(.*)$ { 
    if (-f $request_filename) { 
     break; 
    } 
    proxy_set_header Host $http_host; 
    proxy_pass http://127.0.0.1:3000; 
} 
} 
+0

그냥'(-f $ REQUEST_FILENAME) 경우'문과 나누기를 제거 찾을 수 있습니다? 나는 실제로 무엇이 효과적인지, 그렇지 않은지, 그리고 당신의 필요가 무엇인지 알지 못합니다. – mekwall

+0

노드로'index.html'을 제공하지 않는 이유가 있습니까? 그렇게하면 일이 더 쉬워 질 것입니다. – mekwall

+0

nodejs로 index.html을 제공하지 않는 이유는 정적 html입니다. 이 프로젝트는 아약스 구동 응용 프로그램이며 nodej와 함께 json 만 제공하려고합니다. – Daniel

답변

12

:

여기에 내 현재의 구성입니다. 올바른 방법은 try_files을 명명 된 위치와 함께 사용하는 것입니다.

다음과 같은 구성을 시도해보십시오

# IP which nodejs is running on 
upstream app_x { 
    server 127.0.0.1:3000; 
} 

# nginx server instance 
server { 
    listen 80; 
    server_name x.x.x.x; 
    #access_log /var/log/nginx/x.log; 

    location/{ 
     root /var/www/x/public; 
     index index.html index.htm index.php; 
     try_files $uri $uri/ @node; 
    } 

    location @node { 
     proxy_set_header Host $http_host; 
     proxy_set_header X-Forwarded-For $remote_addr; 
     proxy_pass http://app_x; 
    } 
} 

참고 : 당신이 당신의 proxy_ pass에서 그것을 사용해야 정의 업스트림있을 때. 또한 프록시 할 때 항상 X-Forwarded-For 헤더를 추가하십시오.

+0

고마워 이것이 완벽하게 작동합니다. – Daniel

1

문제가 응용 프로그램 경로에 있는지 궁금합니다. nodejs와의 nginx의 전체 구성을위한 toontuts 블로그에서 다음 코드 발췌를 찾아주세요, 당신은이 link

upstream subdomain.your_domain.com { 
  server 127.0.0.1:3000; 
} 
  
server { 
  listen 0.0.0.0:80; 
  server_name subdomain.your_domain.com; 
  access_log /var/log/nginx/subdomain.your_domain.access.log; 
  error_log /var/log/nginx/subdomain.your_domain.error.log debug; 
  
  location/{ 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-NginX-Proxy true; 
  
    proxy_pass http://subdomain.your_domain.com; 
    proxy_redirect off; 
  } 
}