File upload with nginx and node.js - node.js

I am trying to configure nginx to manage files upload for node.js app.
I have followed this tutorial: https://coderwall.com/p/swgfvw/nginx-direct-file-upload-without-passing-them-through-backend
I have made it with the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /upload {
auth_request /upload/authenticate;
limit_except POST { deny all; }
client_body_temp_path /tmp/;
client_body_in_file_only on;
client_body_buffer_size 128K;
client_max_body_size 1000M;
proxy_pass_request_headers on;
proxy_set_header X-FILE $request_body_file;
proxy_set_body off;
proxy_redirect off;
proxy_pass http://localhost:3000/uploads;
}
location /upload/authenticate {
internal;
proxy_set_body off;
proxy_pass http://localhost:3000/auth/isAuthenticated;
}
}
And I did the test with Postman as follows:
Upload post request
The post request :
POST /upload HTTP/1.1
Host: localhost
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="image"; filename="pic.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
It works fine and nginx uploads the image in the /tmp directory.
The problem is that the file is renamed as "0000000001" and when I rename it manually as "pic.jpg" and try to open it the viewer prompts "Error interpreting JPEG image file (Not a JPEG file: starts with 0x2d 0x2d)".
And when I run the file command(file pic.jpg) it returns: "pic.jpg: data".

Could you check the Postman version?
In my environment, Postman(v3.2.8) has "binary" radio button on request method.
According the blog post, "clientbodyinfileonly" method is incompatible with multi-part data and supports binary data upload only.
So please retry request with binary mode(Postman or another method, e.g. XHR2).

Related

Configure NGINX for multiple odoo instances

i have installed two odoo instances on my VPS, now i'm trying to configure nginx to use both domains with their ports, i am a beginner in nginx, i tried searching in the web but nothing is clear enough, i followed a guide on how to install odoo but it only shows nginx configuration for a single domain.
this is the config i'm currently using :
upstream odooserver {
server 127.0.0.1:8050;
}
server {
listen 80;
server_name www.domain.com;
access_log /var/log/nginx/odoo_access.log;
error_log /var/log/nginx/odooe_error.log;
proxy_read_timeout 720s;
proxy_connect_timeout 720s;
proxy_send_timeout 720s;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_redirect off;
proxy_pass http://odooserver;
}
location ~* /web/static/ {
proxy_cache_valid 200 90m;
proxy_buffering on;
expires 864000;
proxy_pass http://odooserver;
}
gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
gzip on;
}
it's working now after editing the config file like the others suggested, and it turned out that there was something wrong with the DNS settings.

Linux Nginx Reverse Proxy Does Not Serve Custom error.html [duplicate]

I have a Sinatra application hosted with Unicorn, and nginx in front of it. When the Sinatra application errors out (returns 500), I'd like to serve a static page, rather than the default "Internal Server Error". I have the following nginx configuration:
server {
listen 80 default;
server_name *.example.com;
root /home/deploy/www-frontend/current/public;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_pass http://127.0.0.1:4701/;
}
error_page 500 502 503 504 /50x.html;
}
The error_page directive is there, and I have sudo'd as www-data (Ubuntu) and verified I can cat the file, thus it's not a permission problem. With the above config file, and service nginx reload, the page I receive on error is still the same "Internal Server Error".
What's my error?
error_page handles errors that are generated by nginx. By default, nginx will return whatever the proxy server returns regardless of http status code.
What you're looking for is proxy_intercept_errors
This directive decides if nginx will intercept responses with HTTP
status codes of 400 and higher.
By default all responses will be sent as-is from the proxied server.
If you set this to on then nginx will intercept status codes that are
explicitly handled by an error_page directive. Responses with status
codes that do not match an error_page directive will be sent as-is
from the proxied server.
You can set proxy_intercept_errors especially for that location
location /some/location {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_pass http://127.0.0.1:4701/;
proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
error_page 400 500 404 ... other statuses ... =200 /your/path/for/custom/errors;
}
and you can set instead 200 other status what you need
People who are using FastCGI as their upstream need this parameter turned on
fastcgi_intercept_errors on;
For my PHP application, I am using it in my upstream configuration block
location ~ .php$ { ## Execute PHP scripts
fastcgi_pass php-upstream;
fastcgi_intercept_errors on;
error_page 500 /500.html;
}
As mentioned by Stephen in this response, using proxy_intercept_errors on; can work.
Though in my case, as seen in this answer, using uwsgi_intercept_errors on; did the trick...

MEAN stack deployment on server

I create AWS instance and installed Nginx server for my project. Now for angular I create ang.conf and for node create node.conf file in site-available. Share my conf file
ang.conf
server {
listen 80;
server_name IP;
location / {
root project_path;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 403 /403.html;
# To allow POST on static pages
error_page 405 =200 $uri;
}
node.conf
server {
listen 3004;
server_name IP;
location / {
proxy_pass http://IP:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
My node server working fine. I can use my api through postman with the port for example http://MY_YP:3000. But in angular site when I go to browser and go login page and click on submit button not connect to node js server. When I check my response in network it return like this.
Response
HTTP/1.1 200 OK
ETag: W/"5b486d9c-848"
Content-Type: text/html
Date: Fri, 13 Jul 2018 09:56:38 GMT
Last-Modified: Fri, 13 Jul 2018 09:15:08 GMT
Server: nginx/1.10.3 (Ubuntu)
Connection: keep-alive
Content-Encoding: gzip
Transfer-Encoding: Identity
I don't what's wrong in this code. Please suggest me how to handle this.
Finally got the answer. I have to change my nginx.conf file.
events {
worker_connections 4096; ## Default: 1024
}
http {
# Change this depending on environment
upstream api {
server 192.168.0.1:9998;
#put here node.js ip with port
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
location / {
# If you want to enable html5Mode(true) in your angularjs app for pretty URL
# then all request for your angularJS app will be through index.html
try_files $uri /index.html;
}
# /api will server your proxied API that is running on same machine different port
# or another machine. So you can protect your API endpoint not get hit by public directly
location /api {
proxy_pass http://api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#Static File Caching. All static files with the following extension will be cached for 1 day
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1d;
}
}
}

How to respond with 404 from Node.js when serving static files with NGINX?

I have a Node.js app running on port 3000 and using NGINX as a proxy. Static files are also being served by NGINX. The conf in sites-enabled:
server {
listen 80;
server_name myapp.dev;
location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
root /srv/nodejs/myapp/public;
access_log off;
expires max;
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Everything is working fine at the moment. But when a non-existing static file is requested (/css/doesntexist.css) I get a the default NGINX 404 page.
I know it's possible to redirect the user tot a custom 404 page (eg. /404.html), but I want to keep the URL pointing to the non-existing path while displaying a custom 404 from my Node app.
Any ideas?
Got it working with the following.
location ~ ^/(img/|js/|css/|robots.txt|favicon.ico) {
access_log off;
expires max;
error_page 404 = #not_found;
}
location #not_found {
proxy_pass http://127.0.0.1:3000;
}
Which is based on an example from the NGINX documentation for the error_page option.

Nginx proxy_pass and absolute paths

I'm trying to run some nodejs app on a server (Ubuntu 14.04), using Nginx and i'm almost done. Here's my server configuration (/etc/nginx/sites-available/default):
server {
listen 80;
server_name my_domain.com;
location /test1 {
proxy_pass http://127.0.0.1:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /test2 {
proxy_pass http://127.0.0.1:5001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I've got several apps running, and they all works well, i can acces them with http://my_domain.com/test1, http://my_domain.com/test2, etc...
The problem is that inside one of this apps i've got several absolute paths:
e.g. Home
or (inside express)
res.redirect('/');
This redirects don't go to http://my_domain.com/test1 but they go to http://my_domain.com/
Is there a way, through nginx configurations, to tell the app that the root location is actually http://my_domain.com/test1?
I'm really new to nginx and to virtual hosts in general, i'm trying to learn... Any help would be appreciated.
EDIT:
The result of curl -I http://127.0.0.1:5000 is:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 1376
ETag: W/"560-GGm/YYltkhKxiOVNZ99jqQ"
set-cookie: connect.sid=s%3AkZYCqLKvbhHhK3W8ECBN8G91s41paws4.ekLWefrd3NdQatT3VzFNozfnFs65YBEW9k9MNTdbQT0; Path=/; HttpOnly
Date: Sat, 15 Aug 2015 13:13:20 GMT
Connection: keep-alive
As you can see i don't get a Location Header...
By the way, i managed to solve the problem using subdomains, that seem to work as i expected... Anyway an answer would be appreciated, since i might need it in the future.
Use curl to check the header Location value which is used by client to redirect. e.g. curl -v http://127.0.0.1:5001/myappp
Use proxy_redirect to change the Location header

Resources