# How to set up nginx for file uploading

**URL:** <https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623>\
**Category:** NGINX\
**Created:** [December 11, 2015, 1:14pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623 "2015-12-11T13:14:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [December 11, 2015, 1:14pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/1 "2015-12-11T13:14:30Z")

</div>

Hi everyone!

I am having some trouble setting up nginx for file uploading.

I am using nginx as a proxy in front of my nodejs server (that has hapi  
as  
server framework).

Here is the nginx.conf file’s portion for this server:  
[http://dpaste.com/0VJKE5K](http://dpaste.com/0VJKE5K)

The problem is that I get

> No ‘Access-Control-Allow-Origin’ header is present on the requested  
> resource. Origin ‘[https://test.project.com](https://test.project.com)’ is therefore not allowed  
> access.

immediately after sending the pre-flight request when uploading the  
file.

Can anyone please help me?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Vlad\_Fulgeanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@Vlad\_Fulgeanu](https://www.ruby-forum.com/u/Vlad_Fulgeanu)\
**Post date:** [December 11, 2015, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/2 "2015-12-11T15:28:16Z")

</div>

Your config doesn’t appear to add any Access-Control-Allow-Origin  
header,  
so unless your backend is adding this, you will need to add an  
appropriate Access-Control-Allow-Origin header.

---

<div class="post-metadata">

**Author:** ![Vlad\_Fulgeanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@Vlad\_Fulgeanu](https://www.ruby-forum.com/u/Vlad_Fulgeanu)\
**Post date:** [December 11, 2015, 3:51pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/3 "2015-12-11T15:51:03Z")

</div>

I added these in “location /upload/preview”:

add\_header ‘Access-Control-Allow-Origin’ ‘[https://test.project.com](https://test.project.com)’;

> add\_header ‘Access-Control-Allow-Headers’ ‘Authorization, Content-Type,  
> X-Requested-With, Cache-Control, If-None-Match’;

And now it gives me:

No ‘Access-Control-Allow-Origin’ header is present on the requested

> resource. Origin ‘[https://test.project.com](https://test.project.com)’ is therefore not allowed  
> access. The response had HTTP status code 503.

As a response to the actual request (not the pre-flight)

On Fri, Dec 11, 2015 at 4:27 PM, Richard S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Vlad\_Fulgeanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@Vlad\_Fulgeanu](https://www.ruby-forum.com/u/Vlad_Fulgeanu)\
**Post date:** [December 11, 2015, 5:08pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/4 "2015-12-11T17:08:10Z")

</div>

"The response had HTTP status code 503. "

It looks your backend is failing, as it’s returning a HTTP/503 error and  
likely not including the correct headers. You should look into why your  
backed is returning a 503 as this doesn’t seem like an nginx issue any  
more.

---

<div class="post-metadata">

**Author:** ![Vlad\_Fulgeanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@Vlad\_Fulgeanu](https://www.ruby-forum.com/u/Vlad_Fulgeanu)\
**Post date:** [December 12, 2015, 6:11pm UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/5 "2015-12-12T18:11:48Z")

</div>

The problem is that I have tested the application locally (without  
nginx)  
and the uploading works just fine.  
That’s why I think it has something to do with de way nginx is  
configured.

On Fri, Dec 11, 2015 at 6:07 PM, Richard S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Vlad\_Fulgeanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@Vlad\_Fulgeanu](https://www.ruby-forum.com/u/Vlad_Fulgeanu)\
**Post date:** [December 14, 2015, 10:44am UTC](https://www.ruby-forum.com/t/how-to-set-up-nginx-for-file-uploading/243623/6 "2015-12-14T10:44:16Z")

</div>

I managed to make some changes (I was using another config file,  
although “nginx -t” was showing the correct file, witch is very  
strange) and I don’t get HTTP 503 back.  
The config file now looks like this: [http://dpaste.com/18J54VV](http://dpaste.com/18J54VV)  
The problem I have now is that I get

> 500 (Internal Server Error)

as a response to the POST request.

I can see that the files are actually uploaded to  
“/home/project/previews/uploaded-files”, but for whatever reason they  
don’t get passed to the backend server, because the backend doesn’t  
log anything (though it says that it’s an Internal Server Error).
