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;
}
}
}
Related
I am trying to setup a local server for local network use. The React app works fine and can be access from another computer on the network. The problem is, the API endpoints I created is returning 404 error (but not the Nginx 404). It's being treated like a regular Reactjs app internal page.
Here is my Nginx conf file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
# This is the react app
location / {
try_files $uri /index.html;
}
# This is the restful api
location /api {
proxy_redirect off;
default_type application/json;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Content-Type: application/json;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;
proxy_pass http://localhost:5000;
}
}
Here's my React app `package.json:
{
"name": "React APP",
"version": "0.1.0",
"private": true,
"dependencies": {
...
},
"scripts": {
...
},
"devDependencies": {
...
},
"homepage": "."
}
From my localhost machine, I am able to visit http://localhost/ and it shows me my ReactApp just fine, however, http://localhost/api/v1/ and all its endpoints aren't mapping correctly. But if I visit http://localhost:5000, I am able to access my api just fine too.
Can somebody tell me what I did wrong?
The applications run on different ports, 80 for React Application and 5000 for NodeJS server. Have two separate configurations for the two applications under the conf.d folder i.e. react.conf below
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
# This is the react app
location / {
try_files $uri /index.html;
}
}
and for the NodeJS endpoint you can have i.e nodejs.conf
server {
listen 80 default_server;
server_name nodeapi.com;
# Redirect all HTTP to HTTPS
location / {
proxy_redirect off;
default_type application/json;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_a dd_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Content-Type: application/json;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;
proxy_pass "http://localhost:5000";
}
}
I am using NgInx reverse proxy for my expressjs website with HTTPS. website is getting accessed with https without any issue and requests are getting at Nodejs server but problem is with Static content.
All static contents(JS, CSS, Images, fonts etc.) are throwing 403 forbidden error.
Below is my ngInx configuration file content:
server {
listen 80;
listen 443 ssl;
ssl on;
ssl_certificate /home/ec2-user/certs/myapp.bundle.crt;
ssl_certificate_key /home/ec2-user/certs/myapp.key;
root /home/ec2-user/myapp;
server_name example.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
#try_files $uri $uri/ /index.php?$args ;
#try_files $uri $uri/ =404;
proxy_pass https://example.com: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;
}
}
I am using amazon EC2 and CentOS.
Please help. Thanks in advance.
location / {
proxy_pass https://example.com: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;
}
You are redirecting all traffic to to nodejs. Thats means expressjs will serve static content.
To serve static files, use the express.static built-in middleware function in Express.
Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. For example, use the following code to serve static files in a directory named public:
app.use(express.static('public'))
More info here: https://expressjs.com/en/starter/static-files.html
You are trying to server static files from your NodeJS app which is not right approach since you are using Nginx. Add a below kind of block to you config
location ~ \.(css|gif|png|jpe?g|ico|js|swf|ttf|woff) {
expires 30d;
root /home/ec2-user/myapp;
try_files $uri =404;
}
You might need to adjust the root folder based on how you are using your assets. But if you are using it same as /home/ec2-user/myapp then you can skip adding this line
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.
I've set up an Nginx server which does a proxy_pass to a Node server if no html files are found. I can access my static html file just fine but when I hit the Node server I get a 403: Forbidden. Here is my Nginx conf if that helps:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
upstream mysite_upstream {
server 127.0.0.1:8000;
keepalive 64;
}
server {
listen 80;
server_name staging.mysite.org;
root /var/www/staging.mysite.org/public;
access_log /var/logs/staging.mysite.org.access.log;
error_log /var/logs/staging.mysite.org.error.log;
error_page 404 /404.html;
error_page 500 503 /500.html;
location ~ ^/(images/|javascript/|js/|css/|style/|flash/|robots.txt|sitemap.xml|humans.txt|favicon.ico) {
access_log off;
expires max;
}
location #node {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://mysite_upstream;
}
location / {
try_files $uri $uri/ $uri.html #node;
}
}
I should mention that if I take out try_files and just proxy_pass directly from / the Node app is served up but I get a 404 for all my static html files. Also, this is working fine on my local machine (osx).
I have a nodejs server and SSL enabled nginx on 2 separate machines. Request/response all work properly, however I have some problems getting nginx to cache stuff. My server configuration is below. Initially, I had the proxy cache statement in the 'location /' block, and at the time it was caching only my index page. I read that nginx won't cache requests with set-cookie headers, so I ignored them as well (although it didn't stop my index page from getting cached earlier). I tried fiddling with this for a whole day, but couldn't get nginx to cache my js and css files. All such requests are getting routed back to my node server. Access logs and error logs don't have any unusual entries. What am I doing wrong?
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /webserver/nginx/credentials/cert;
ssl_certificate_key /webserver/nginx/credentials/key;
ssl_session_cache shared:SSL:10m;
location ~ .*\.(ico|css|js|gif|jpe?g|png)$ {
proxy_pass http://somewhere:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect http:// https://;
proxy_ignore_headers "Set-Cookie";
proxy_cache one;
proxy_cache_valid 200 1d;
proxy_cache_valid any 1m;
expires 7d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
location / {
proxy_pass http://somewhere:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect http:// https://;
}
}
This is what I'm using (I don't have SSL enabled but I don't think that is the problem). You're missing the try_files line that tells nginx to look for the files in the root before passing off to the proxy. Also, it's not really a caching problem - none of the static file requests should ever be hitting your node.js backend with this configuration.
server {
root /public;
listen 80;
server_name _;
index index.html index.htm;
charset utf-8;
# proxy request to node
location #proxy {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3010;
proxy_redirect off;
break;
}
location / {
try_files $uri.html $uri $uri/ #proxy;
}
# static content
location ~ \.(?:ico|jpg|css|png|js|swf|woff|eot|svg|ttf|html|gif)$ {
access_log off;
log_not_found off;
add_header Pragma "public";
add_header Cache-Control "public";
expires 30d;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
error_page 404 /404.html;
location = /404.html {
}
}