Debugging NGINX site config file - nginx

I'm having trouble setting up Nginx site configuration file.
So basically I'm serving 3 sites:
Dashboard /dashboard (react-based, operates at xxx/xxx/build/index.html)
API /api (node-based, operates using a proxy-pass and it works perfectly
Homepage / (static, operates at xxx/yyy/zzz/index.html)
My Nginx file looks as follows:
server {
listen 80;
server_name mydomain.com;
server_name xxx.xxx.xxx.xxx;
access_log /var/log/nginx/xxxxx;
error_log /var/log/nginx/xxxxx;
location /dashboard/ {
root /var/www/xxxxx/build;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://localhost:5050/;
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 / {
root /var/www/xxxxxx;
index index.html;
try_files $uri /$uri /index.html;
}
}
The problem lays mainly in the /dashboard path. For some reason it redirects to the homepage and never returns the actual dashboard.
I'm quite inexperienced in Nginx, so I'm not quite sure how to even debug it.

Just a guess.
root will add your location /dashbaord behind /var/www/xxxxx/build like so:
/var/www/xxxxx/build/dashboard
That location does not exists so it tries /index.php at the end of try_files which is an "absolute path" to your homepage?
I would try using alias instead of root (which will not add /dashboard to your root path) and/or try to fix your last parameter in try_files like so:
try_files $uri $uri/ ./index.html;

Related

When accessing to the path as backend from the path as frontend, wish redirecting using Nginx to show wordpress page by frontend path

Those are the environment.
■Frontend
Server: Vercel
■Backend
Server: EC2
Middle: Nginx
note: wordpress is constructed in EC2
What I want is
① Accessing to https://example2.jp/hoge which is backend
② Redirecting to https://example.jp/hoge which is frontend to use Nginx showing wordpress page
I tried rewrite to achieve, but it couldn't work at all.
server {
listen 80;
server_name example2.jp;
root /var/www/example2/current/public;
location / {
proxy_pass http://example2;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /hoge/ {
root /var/www/html;
index index.php;
try_files $uri $uri/ /hoge/index.php?$args;
expires 7d;
rewrite ^ $scheme://example.jp/hoge/ permanent;
}
}
Could you give me tips?

Nginx: How to rewrite url for a particular directory only

I am using nginx for reverse proxy. I am rewriting a directory using an alias. On my alias directory, I have Angular 2 build files which are using html5 routing. So when user will refresh the page it should rewrite to the index.html present in that directory only.
"control-pane is overwrite by an alias"
I already tried the "try_files $uri $uri/ /index.html =404;"
but this was referring to index.html inside main directory.
I also tried to tried to give an absolute path but still, it was not working.
server {
listen 80;
server_name xx.xxx.xx.xxx;
location / {
proxy_pass http://localhost:4000;
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 /control-panel {
alias /var/www/html/soundoff-admin-dev/dist;
# here rewrite should override with index.html inside "dist" directory
}
}
Borrowed from https://stackoverflow.com/a/50863128/1264360
Try,
location /control-panel {
root /var/www/html/soundoff-admin-dev/dist;
try_files $uri $uri/ /control-panel/index.html;
}

Configure Nginx to serve a node js app from a location within root directory

I am trying to serve a Node JS app alongside some static sites that are already functioning and only serving static content (/insta-app is one of the apps, there is another nginx file with another server block for the other ones which sit on different subdomains). I am successfuly getting the server.js app when I navigate to the URL /nodejsapp. The problem is that all the static content the app requires does not get served and comes up as a 404 (images, js files and css). I wrote a location block for nodejsapp/dist where the static content exists but this did not fix the problem. The content is being requested by a handlebars template that gets successfully called and is sitting in a views folder in the root. I am running the whole thing on an ubuntu server, and the node app is running through pm2 which is working fine when I request curl localhost:3000. How can I get the static content into the server?
Nginx server block:
server {
listen 443 ssl default_server;
root /var/www;
index index.html index.htm server.js;
server_name uat.com www.uat.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/uat.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/uat.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ /.well-known{
allow all;
}
location /insta-app{
alias /var/www/insta-app/html;
allow all;
}
location /nodeJsApp{
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:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $proxy_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# attempt to serve the static content of the app
location /nodeJsApp/dist{
root /var/www/sydney-sevens/dist;
try_files $uri $uri/ =404;
}
}

cURL works but browser (through NGINX) won't load

I am using NGINX and Unicorn for my Rails app. I've hit a brick wall and am not sure what is happening.
I have set NGINX logging to full debug. When I attempt to browse to my site home page, I get problem loading page and nothing in the NGINX error log. When I use cURL to get to the same page, it works perfectly and the log is full of helpful information. I'm hoping this is an obvious error in my NGINX configuration file:
upstream unicorn-soup {
server unix:/home/soup/app/tmp/sockets/unicorn.sock;
}
server {
listen 80 default;
listen [::]:80 default;
root /home/soup/app/current/public;
server_name soup.quote2bill.com;
try_files $uri/index.html $uri.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded_Proto $scheme;
proxy_redirect off;
proxy_pass http://unicorn-soup;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
}
Note I even tried setting this to the default site and browsing to the IP address but it didn't help.
Thanks for your time and suggestions on how to troubleshoot and/or fix.
Fixed! I had dropped the location directive for the try line.
Corrected:
location / {
try_files $uri/index.html $uri.html $uri #unicorn;
}

Using nginx as reverse proxy for apache and want more htaccess control

I'm using nginx in a reverse proxy configuration with apache2. I have used other, preconfigured web servers in this way and enjoyed complete control over redirects from an .htaccess file.
My current configuration does not allow for this. First, I'll explain what happens.
Let's say I want to redirect /google to http://google.com. I add the following line to my .htaccess file.
redirect 307 /google http://google.com
I know it's working because I can test with curl from my server. This is hitting apache directly, behind the proxy.
curl -I localhost:8080/google
and I get a 307 as I would expect.
But if this request hits nginx from the outside, nginx knows there is no such file in the web root and responds 404.
Is there a configuration change I can make to remedy this?
Here's my nginx configuration file for this vhost.
server {
listen 80;
root /var/www/mywebsite.com/html;
index index.php index.html index.htm;
server_name mywebsite.com;
access_log /var/log/nginx/mywebsite.com.access.log;
error_log /var/log/nginx/mywebsite.com.error.log;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\. {
deny all;
}
}
It seems that I might need to take the reverse approach and proxy everything but exclude certain file types from the proxy.
Thanks for advice folks!
If you want to forward that request to /google to apache, then just change the location ~ \.php$ pattern to include the request you desire:
location ~ (/google/?)|(\.php)$ {
On the other hand if you want nginx to handle the redirect you can add new rule:
location ~ /google {
return 307 http://google.com;
}
Just make sure to put it before the location block which contains try_files.
EDIT:
To forward all requests that don't hit a file use this:
location /
{
if (!-f $request_filename) #test if a static file does not exists
{
# forward the request if there is no file to serve:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
Additionally if you want for nginx to also serve entire directories then add another condition:
!-d $request_filename
Also note that if you want only specific files to be served then instead location / use the pattern to match those files. For example if you want to serve only jpg's and css's use
location ~ \.(jpg|css)$
EDIT2: Here you have a simplified version of your script, which is also more robust - lets you serve only the types of files you want:
server {
listen 80;
server_name test.lcl;
root /home/www/test;
location / {
try_files $uri $uri/ #apache;
}
location #apache
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}

Resources