NGINX 404 error when accessing nested React App routes - node.js

Background/Objectives:
Hi everyone, first time poster here!
I am currently trying to configure NGINX so that it -
Serves a React App when specific routes are hit e.g. /auth/
Proxies all traffic on the /api route to a Node back-end
Serves a Wordpress site on all other routes
Issue:
I have an /auth location block defined in my NGINX config file that points to a React App contained in the folder /home/ubuntu/app/client/build.
If I visit the /auth route, NGINX correctly serves the React App.
However, when I make a HTTP request to a nested route (e.g. /auth/login) I get a 404 error.
I have tried stripping the NGINX config file back to just contain the /auth location block but the issue still persists. Therefore, I don't think it has to do with other settings inside the file.
Specifying exact routes is not a viable solution as many of the other routes I am yet to add have dynamic nested values e.g. /users/someuniqueid.
Configuration File
Hopefully the following sheds some light on what I am doing wrong. Note, I have removed the server name for privacy reasons so please don't assume I have used that actual name :)
server {
listen 80;
server_name myipaddress;
root /home/ubuntu/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /css {
alias /home/ubuntu/app/client/build/css;
}
location /media {
alias /home/ubuntu/app/client/build/media;
}
location /static {
alias /home/ubuntu/app/client/build/static;
}
location /assets {
alias /home/ubuntu/app/client/build/assets;
}
location /auth {
alias /home/ubuntu/app/client/build;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; #Ubuntu 17.10
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myurl.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myurl.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhusername.pem; # managed by Certbot
}

This problem is already mentioned here.
Try replacing the location /auth configuration with this.
location /auth/ {
alias /home/ubuntu/app/client/build;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location / {
alias WORD_PRESS_PATH_HERE;
index index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; #Ubuntu 17.10
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Thanks for reading.

Related

Proper ExpressJS and NGINX reverse proxy configuration for subdirectory

I have a VPS with NGINX installed, it's running a TLD website and I want to run my ExpressJS application from a subdirectory called nlapp. E.g. /var/www/website.com/public_html/nlapp.
Here's the app.js:
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const https = require("https");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/nlapp", function (req, res) {
res.sendFile(__dirname + "/signup.html");
});
My NodeJS application runs without errors and it works fine, but it doesn't seem to serve static content this far. I have disabled express static but it still doesn't work.
Here's my configuration for this right now:
server {
server_name website.com www.website.com;
root /var/www/website.com/public_html/;
index index.html index.php;
# Modsecurity
client_max_body_size 100M;
# Logs
access_log /var/log/nginx/access.log_website.com;
error_log /var/log/nginx/error.log_website.com;
# Don't allow pages to be rendered in an iframe on external domains.
add_header X-Frame-Options "SAMEORIGIN";
# MIME sniffing prevention
add_header X-Content-Type-Options "nosniff";
# Enable cross-site scripting filter in supported browsers.
add_header X-Xss-Protection "1; mode=block";
# Prevent access to hidden files
location ~* /\.(?!well-known\/) {
deny all;
}
# Prevent access to certain file extensions
location ~\.(ini|log|conf)$ {
deny all;
}
# Enable WordPress Permananent Links
location / {
try_files $uri $uri/ /index.php?$args;
autoindex off;
}
# Restrict GoAccess
location /rep_est_status.html {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
}
# Cache for 2 days
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 2d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Deny access to xmlrpc.php
location = /xmlrpc.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
allow 87.71.160.0/19;
deny all;
}
location /nlapp {
alias /var/www/website.com/public_html/nlapp/public;
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;
}
listen 443 ssl http2 default_server;
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # managed by Certbot
My NodeJS application is running on port 3000, and it worked great on my localhost.
But on this remote NGINX server (that I manage) the static content that is in /var/www/website.com/public_html/nlapp/public.
There I have 2 directories: css and images.
When I load the application from my browser, I go to this URL:
https://www.website.com/nlapp and it launches successfully.
The CSS and images are not loading, I've checked Network tab on dev tools and it seems it's trying to load them from the root directory of the website, e.g. https://www.website.com/css instead of https://www.website.com/nlapp/public/css
How do I fix this?
Thank you!
What did I try?
I tried using the alias or root directives under the specified location, as seen in the example above.
It didn't amount to anything, still the same.
Also tried googling and searching more and more for a few hours - couldn't arrive at a conclusion.

Link a domain name without port number to a MEAN stack server hosted by nginx

I have a Ubuntu Server on DigitalOcean, which hosts several websites. I just built a mean.js stack app on my mac, and I plan to deploy it to production, thus to this existing server (though I don't know if I need to create another droplet like here).
I followed this link to install node.js and mongodb, etc. Then, I cloned my own app from the github:
sudo git clone https://github.com/softtimur/myapp.git /opt/myapp
cd /opt/myapp
sudo npm install
npm start
As a result, in a browser, by entering https://xxx.xx.xx.xx:3000/#/home, it communicates well with the server.
Now, I would like to use the domain name I bought from GoDaddy (ie, myapp.io) rather than the IP address to communicate to the server.
I have modified the records in DNS of myapp.io such that it points to the IP address. As a result, https://www.myapp.io leads well to the server, however, it leads to another page set by nginx by default.
Then, I set /etc/nginx/sites-available/myapp.io and /etc/nginx/sites-enabled/myapp.io as follows:
server {
listen 3000;
listen [::]:3000;
root /opt/myopp/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name myopp.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
client_max_body_size 15M;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
After restarting nginx, npm start returns an error: Port 3000 is already in use.
Could anyone tell me if this approach is correct? If yes, how could I fix the error, eg., the nginx config file?
Edit 1: In /etc/nginx/sites-avaiable/default, I have
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name xxx.xx.xx.x;
What you are trying to do is reverse proxy from //www.myapp.io to //xxx.xx.xx.xx:3000. This is achieved by listening on port 80 (or 443) and using proxy_pass to connect with your service running on port 3000. See this document for details.
For an http server, you could use:
server {
listen 80;
server_name myopp.io;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
proxy_pass http://127.0.0.1:3000;
}
}
Obviously you are using https which could be implemented by changing your service to use http on port 3000. Installing your certificates and terminating SSL using nginx. See this document for more.

Getting nginx and node.js to play nice

I have an nginx/node.js server I'm trying to configure. Basically it's just the issue of running 2 web servers on port 80 at the same time. I have www.mysite.com that I need to point to nginx on port 80. But I also have a node.js server that I need api.mysite.com to point to port 8888.
I'm messing around with proxy_pass in my config (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) but with no luck. I also tried this https://stackoverflow.com/a/20716524/605841 with no luck.
If anyone has any tips that would be great. Thanks in advance.
Nginx public dir: /var/www/html. Express app location: /var/www/html/myNodeAppRoot
Here's my /etc/nginx/sites-available/api.mysite.com file (sym linked into sites-enabled):
server {
listen 80;
# server_name ~^(?<login>[a-z]+)\.api\.mysite\.com$;
server_name api.mysite.com$;
location / {
# root /var/www/html/myNodeAppRoot;
# proxy_pass http://unix:/tmp/\$login.api.mysite.com.sock:$uri$is_args$args;
proxy_pass http://unix:/tmp/api.mysite.com.sock:$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
And here's my default.conf file:
#
# The default server
#
server {
listen 80 default_server;
server_name www.mysite.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html;
try_files $uri =404;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Thanks for any help!
I run a bunch of Node.js applications on the same server, while nginx serves some static content. Here's my setup:
# the meteor server
server {
server_name example.com;
access_log /etc/nginx/logs/example.access;
error_log /etc/nginx/logs/example.error error;
location / {
proxy_pass http://localhost:3030;
proxy_set_header X-Real-IP $remote_addr;
}
}
I just repeat this block and change the port for each new Node.js app. Then I run Node.js with a different --port 3030 parameter.
nginx can be configured to use unix sockets, which look like an item in the file system. Node supports these out of the box. This allows you to avoid any issues with ports behind nginx.
A good tutorial for setting up a Node app with nginx and sockets can be found here.

I can access my subdomain URL paths on both my domain and subdomain?

I have a DigitalOcean VPS running nginx which has two websites on it. The two sites on it are: www.ingledow.co.uk and blog.ingledow.co.uk.
My main (www.) domain is predominantly a static site, but my blog (blog.) subdomain runs on Ghost.
Everything works perfectly apart from that I can access my blog from both www. and blog.. For example, here is a blog post at http://blog.ingledow.co.uk/puma-social-club/, but the same blog post can be seen from http://www.ingledow.co.uk/puma-social-club/.
Another point to note is that if you try to go to http://ingledow.co.uk/puma-social-club/ without the www. or blog. it 404s.
The problem lies in having two sites on the same VPS, but not sure if there's a problem with my nginx configs or whether it's problems with my DNS, or both?
The nginx config files are in /sites-available/ and symlinked to /sites-enabled/
I need to get this fixed because it is causing issues with Google search results and SEO.
Here's my DNS:
blog.ingledow.co.uk.conf
# blog.ingledow.co.uk running on Ghost
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ghost;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name blog.ingledow.co.uk;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:2369;
client_max_body_size 10m;
break;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
}
ingledow.co.uk.conf
# ingledow.co.uk.conf
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ingledow.co.uk/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ingledow.co.uk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /phpmyadmin { index index.php; }
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Try adding www. to sever_name ingledow.co.uk; in the ingledow.co.uk server block. e.g. :
server_name www.ingledow.co.uk ingledow.co.uk;
If you don't want to site to be accessed without the www. subdomain prefix you should remove it from the server_name.
Another way to do it is to have the server block as is for the blog, and just use a catch all server block for the main static site.

Cannot access sqlbuddy on nginx server

I installed sqlbuddy following the guide by arstechnia, but I cannot seem to access sqlbuddy.
This is the setup for /etc/nginx/sites-available/www
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
try_files $uri $uri/ =404;
allow 192.168.1.0/24;
allow 127.0.0.1;
deny all;
}
location ~ \.php$ {
try_files $uri =404;
allow 192.168.1.0/24;
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
location ~ /\. {access_log off; log_not_found off; deny all; }
location ~ ~$ {access_log off; log_not_found off; deny all; }
location ~ /sqlbuddy/.*\.php$ {
allow 192.168.1.0/24;
allow 127.0.0.1;
deny all;
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
When I try to access sqlbuddy via 192.168.1.128/sqlbuddy I get this page from chrome:
http://imgur.com/8pomz3m
Nginx access log shows no record of me trying to access sqlbuddy but does record me accessing the index page and 192.168.1.128/phpinfo.php
No errors present in nginx error log either.
I tried individually commenting sections of the location ~ /sqlbuddy/... to no avail. Really lost on this one.
Following the same guide I came across with the same problem.
I commented out the location that made all requests to sqlbuddy go through https, I see you don't have it so it shouldn't be a problem.
Try deleting the browser's cache. That dit it for me.
Hope it helps.-
Check your sqlbuddy folder structure, make sure your index is in /sqlbuddy/index.php and not in /sqlbuddy/src/index.php. I had to copy all sources to /sqlbuddy.
also check your permission and owner for sqlbuddy (www-data:www-data).

Resources