2017-12-28 19 views
0

Expressアプリケーションはブラウザに静的ファイルを読み込むことはできませんが、カールで動作します。私はウェブサーバーとしてnginxを使用します。アプリケーションは正常に動作しますが、静的ファイルはありません。私はここで間違って何をしていますか?Express/Nginxで静的ファイルを読み込めません

App.js

... 

App.use(cors()) 
App.use("/data", express.static(__dirname + '/data')); 

App.use('/api', require('./routes/api')) 

App.listen(1337) 

nginxの

server 
{ 
     listen x.x.x.x:80; 
     server_name example.com www.example.com ; 

     location /api { 

       proxy_pass http://x.x.x.x:1337; 

       ... 
     } 

    location/{ 
       return 301 https://$server_name$request_uri; 
     } 

} 

server 
{ 
     listen x.x.x.x:443 ssl; 
     server_name example.com www.example.com ; 
    root /path/to/public_html; 
     index index.php index.html index.htm; 
     ssl on; 
     location /api { 
       add_header X-Cache "HIT from Backend"; 

       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
       proxy_set_header Host $http_host; 

       proxy_pass http://x.x.x.x:1337; 

       ... 
     } 
} 

カールはokです:

http://127.0.0.1:1337/data/pic.png 

しかし、ブラウザではありません。

http://example.com/api/data/pic.png 

ルータ:

App.use('/api', require('./routes/api')) 
+1

http://x.x.x.x:1337/data/pic.pngにリダイレクトされます、このnginxの設定ファイルを試してみて、私のルータである@Farnabaz、あなたのブラウザのアドレス – Farnabaz

+0

に/ api' '削除 – user1788781

答えて

1

それはあなたがブラウザに間違ったアドレスを要求http://example.com/api/data/pic.png

location /api { 
    rewrite /api(.*) /$1 break; 
    proxy_pass http://x.x.x.x:1337; 

    ... 
} 
+0

残念ながら、それは動作しません – user1788781

+0

'http://example.com/api/dataへのリクエストのリクエスト/レスポンスヘッダーとは何ですか?( '/ api'、require( './ routes/api'))/pic.png'? – Khang

関連する問題