EmberJS & Nginx - Routes return 404's - nginx

It seems like a simple problem but I cannot find any official information about how to fix it.
The problem: Deploying an EmberJS app to Nginx - all routes return 404's.
The root route / works just fine. However any route besides / fails & returns 404's. Here's how I have my nginx.conf setup:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/ivo/myapp;
index index.html;
location / {
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html?/$request_uri;
}
}
}
This config is largely based on the nginx.conf that comes with the Nginx Docker image, which I am using.
From what I understand about Nginx, the location / block should be handling my routes, but for some reason it is not.
As a side note, I am shocked that EmberJS does not have any documentation about this at all. I even tried their Nginx config from their deployment section and it too did not work - https://ember-cli.com/user-guide/#deployments

Found & "fixed" the problem. The underlying issue is that the information provided on the Nginx DockerHub page is misleading. It says:
"If you wish to adapt the default configuration, use something like the following to copy it from a running nginx container"
And it provides this line as an example:
"COPY nginx.conf /etc/nginx/nginx.conf"
However, the file located at /etc/nginx/nginx.conf was not the file I needed. Instead, the file I needed to change/replace was located here : "/etc/nginx/conf.d/default.conf"
The code I pasted above in the Question portion worked fine; it was the location that was wrong.

Related

CentOS displays NGINX page instead of flask app?

I try to deploy a Flask App on centOS with NGINX. The Flask app is served on 127.0.0.1:5000 and is also accessible with curl 127.0.0.1:5000.
I tried to keep it simple and removed all NGINX config files in conf.d and just use nginx.conf. This is the whole content of the nginx.conf file:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
However, NGINX still shows its default page and there seems to be nothing in the world or at least in the nginx.conf file to change this. Is there anything in NGINX that still might require reconfiguration or anything on centOS that could lead to the problem?
I solved it. It was indeed open NGINX commands.
After I ran the following commands it worked:
sudo systemctl stop nginx
sudo systemctl enable nginx
sudo systemctl restart nginx
Maybe it helps someone in the future.

How to setup nginx to create a sub-domain?

I have one domain myartistbook.in, which is working fine. I want to create a sub-domain for it named adminpanel.myartistbook.in. The steps I followed are:
Added sub-domain name to /etc/hosts
127.0.0.1 localhost
***.***.***.180 myartistbook
***.***.***.180 adminpanel.myartistbook
edited /etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name myartistbook.in www.myartistbook.in;
root /root/krim.com;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /root/sites/adminpanel.com;
server_name adminpanel.myartistbook.in www.adminpanel.myartistbook.in;
index index.html;
location / {
try_files $uri $uri/;
}
}
}
Added a file named adminpanel.com inside /etc/nginx/sites-available.
server {
listen 80;
server_name www.adminpanel.myartistbook.in;
server_name ***.***.***.180;
root /root/sites/adminpanel.com;
index index.html;
access_log /var/log/nginx/adminpanel.com.access.log;
error_log /var/log/nginx/adminpanel.com.error.log;
location / {
try_files $uri /index.html =404;
}
}
Linked the above server block into /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/adminpanel.com /etc/nginx/sites-enabled/adminpanel.com
Restarted nginx with
sudo service nginx restart
While accessing the site I'm getting error adminpanel.myartistbook.in’s server IP address could not be found.
Am I missing any step?
Together with server-side configurations involving domains and subdomains in server_name, you need to make sure that your DNS records for myartistbook.in are properly configured as well.
Have you added a DNS record to point adminpanel.myartistbook.in to your server? You can either add an A record or a CNAME record for this. Looking at a quick check using whatsmydns.net for adminpanel.myartistbook.in, it seems you missed this step. Please check the documentation of your domain registrar on how to do this.
After adding the records, do a quick check again using whatsmydns.net if your CNAME/A record has taken effect.
In your step 3, modify the server_name to adminpanel.myartistbook.in without the www, unless this is actually your intention to use the whole www.adminpanel.myartistbook.in, which I don't think is the case here because you attempted to access it without the www:
adminpanel.myartistbook.in’s server IP address could not be found.
Also, it's probably good to use a different server {} block when referring to IP addresses as server_name then redirect to the domain/subdomain. However, redirecting from IP Address going to your domain/subdomain may be not be the priority now. Suggest to remove that line in the meantime.
Then run a quick nginx -T to check for errors in your configuration. If the check is successful, reload your nginx web server and try accessing the subdomain again.
Hope that helps!

How nginx pick the configuration order?

In my /etc/nginx/nginx.conf file I have config. as:-
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
Now, I don't like to pollute above default nginx.conf file, so I kept configuration in /etc/nginx/conf.d/default.conf as:-
worker_processes 2;
events {
worker_connections 2048;
}
My question is, In above scenario will nginx override or pick configuration for worker_processes and worker_connections from default.conf file or nginx.conf file ? Also, I would like to know how nginx processes configuration files in short ?
For "how nginx processes configuration files", a simple way to look at it would be:
reading of configuration starts with /etc/nginx/nginx.conf
directives are read from top to bottom
includeing a file inserts it at the location of the include
similar to the way the C preprocessor does
a setting has a scope, such as http, server, or location, in order from wide to narrow
some settings can occur at multiple scope levels
setting a parameter at a narrower scope overrides a setting of the same parameter at a wider scope
conflicting settings within the same scope are rejected
For detail, see the Debian wiki article on Nginx directory structure.
I'm sure that configuration in default.conf will be picked first by nginx, thats why the line "include /etc/nginx/conf.d/*.conf;" is included to override any defaults and add functionality.

Is my NGINX conf file correct

I am new to NGINX and I can't tell if I set my conf file up properly. I am trying to serve an index.html file located in the data/www directory. I did a lot of research and I can't seem to figure it out. I have nginx installed on a ec2 running centOS 6.5. Any pointers on how to get this up and running would be greatly appreciated. I have been trying to wrap my head around this for a while now. Thank you in advance!
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name <my elastic ip address>;
location / {
root /data/www/;
index index.html;
}
}
}
Yes,
Your Configuration file is correct.
NOTE:
working:First check your nginx server is running or not, by typing your ip:port(that you given in conf file) in browser.If running it will display nginx current version details.
ACCESS: you can access your file by using command like this.
http://your_ip_addr:port/application name/file_name
*application name:the name given for that app,
for example
location /html {
......
}
then you application name will become html.

Getting error 502 instead of displaying errors on nginx + php-fpm

I've tried everything and i can't still get to display errors on the browser, I'm using nginx, php-fpm and centOS 6.4
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
this is my nginx config inside conf.d
server {
listen 80;
server_name localhost;
root /server/public;
index run.php;
location / {
try_files $uri $uri/ /run.php;
}
location ~ \.php$ {
fastcgi_index run.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
php-fpm config
catch_workers_output = yes
php_flag[display_errors] = on
php_admin_flag[log_errors] = on
php.ini
display_errors = on
log_errors = on
I'm running CentOS 6.4 on virtualbox with NAT enabled, if i curl localhost from inside the server i get the correct error but if i access the site from the browser outside of the server i get a 502 bad gateway error, if the page I'm loading has no errors everything works fine, am I missing anything?
If you're wondering why you were getting that error I could give you few hints.
error 502 means bad gateway, means that the php server that nginx was trying to proxy to wasn't responding, maybe because php5-fpm wasn't listening to port 9000 and it was using a sock file instead, or could be that php wasn't running at all(if all php files aren't working)
Why did you get different responses from curl and the browser?, well because your server is defined as localhost I'm assuming both methods were not being captured by different server blocks.
The correct method to have fixed this is to check the active virtual hosts, and the php5-fpm listen config,
I think it's just coincidence that install the new php fixed the issue probably because it overwritten the old config file to a new one that worked with the nginx config.
But anyway, I was just trying to give you some hints of what to check if you ever find a similar problem in the future.
Looks like it was my version of php (5.3) that was failing, after updating to php 5.5 it started working

Resources