Nginx not show urls of angular 4 - django

I am deploy my web server in Django with Angular4 into my LightSail server but I have a problem, because I try access to my page example.com and redirect to example.com/home, no problem I see my page without problems, but If I try to reload the page it sends me a message as if the route did not exist, being that in my local if I can do it without problem.
this is my config in vim /etc/nginx/sites-enabled/trackerServer,
I will be honest, I followed a guide since I do not handle much with nginx from
LightSail with Django
server {
listen 80;
server_name 54.175.253.151;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/tracker-web/trackerServer;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/tracker-web/trackerServer/trackerServer.sock;
}
}
I discard that my problem is with gunicorn.
Someone has gone through the same and has managed to solve this problem of routing?

This question has nothing related to django, and it's not an nginx or gunicorn problem.
You need to add LocationStrategy on your providers.
Take a look at this question

Angular's documentation has a section for server configuration. When using the HTML5 style routing you have to redirect all urls to index.html in nginx configuration:
NGinx: use try_files, as described in Front Controller Pattern Web
Apps, modified to serve index.html:
try_files $uri $uri/ /index.html;
OR
Use the HashLocationStrategy as mentioned in another answer.

Related

Nginx shows only Welcome page after changing server_name from IP adress to domain

I use Nginx as Reverse Proxy for a Django project with Gunicorn.
After following this tutorial from Digital Ocean How To Set Up an ASGI Django App I was able to visit my project through the server IP adress in a browser with http.
In the next step I followed the How To Secure Nginx with Let's Encrypt tutorial from Digital Ocean. Now the site was available with http:// and https:// in front of the IP adress.
To redirect the user automatically to https I used code from this tutorial.5 Steps to deploy Django
The outcome is the following file in /etc/nginx/sites-available:
# Force http to https
server {
listen 80;
server_name EXAMPLE_IP_ADRESS;
return 301 https://EXAMPLE_IP_ADRESS$request_uri;
}
server {
listen 80; # manged by Certbot
server_name EXAMPLE_IP_ADRESS;
# serve static files
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/projectdir;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
The redirect to https is working fine, so I assume the changes I made according to the last tutorial are okay.
After the tests with the EXAMPLE_IP_ADRESS as server_name went well I have changed the server_name to my domain in the form www.example.com
When I type the domain in the browser the only result is the Nginx Welcome page. So the connection to the server is successfull but Nginx is loading the wrong server block.
After searching for hours I came across this Question. Here the answer of ThorSummoner worked for me. The comment by mauris under this answer to unlink the default file in the sites-enabled was the command I needed.
unlink sites-enabled/default
(I posted this Q&A because I searched hours for the solution and hope this is reducing the search time for others having a Django project too with the same problem)

nginx throws bad request 400 when accessed through ddns domain

Working perfect when accessed through local web server IP but throws 400 error when accessed through NOIP hostname.
For context, router is configured to forward requests to the web server on that port.
This is the nginx config file:
server {
listen 80;
server_name 192.168.1.64;
#server_name example.ddns.net;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/projectdir;
}
location / {
include proxy_params;
# proxy_pass http://example.ddns.net;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
I tried adding the lines where the hashtags are but to no avail.
Assuming your server is properly configured, edit your setting.py in your Django project with the following:
ALLOWED_HOSTS = ["192.168.1.64"]
For future reference, in my case, although I had ALLOWED_HOSTS configured correctly, the problem was reloading gunicorn/nginx after making changes to the django settings file. The following line solved it:
sudo systemctl restart gunicorn
sudo systemctl restart nginx
Credit where it's due, comments from Iain Shelvington and Gaƫtan GR were spot on, the underlying problem was ALLOWED_HOSTS setting (which in my case was not applied until a gunicorn/nginx restart was done.

React build files giving net::ERR_ABORTED 403 (Forbidden) in Nginx

I'm using nginx and gunicorn to deploy my Django+React web application. When I start the nginx services and try to access the application in the browser, in the browser's console I'm getting an error saying
GET http://server_ip/static/css/2.2546a949.chunk.css net::ERR_ABORTED 403 (Forbidden)
Below is what my nginx conf file looks like
server {
listen 80;
server_name server_ip;
location / {
proxy_pass http://127.0.0.1:8000;
}
location /static/ {
autoindex on;
alias react_build_folder_location;
}
}
Also, I've given read and execute permission to the static files still not able to fix the issue! Can anyone please help me. Kindly excuse if its a repeated question.

nginx working with ip but not domain name

I'm trying to set up a django app with gunicorn and ngix. I followed this tutorial. Everything seems to be working but when I edit the server_name in /etc/nginx/sites-available/project to anything other than the serevr ip address I get the default nginx index page instead of the django app. When this is the server config:
server {
listen 80;
server_name <myserverip>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
everything works as expected (nginx serves the app) when I put the ip address into my browser, but if the I add a domain name to replace the ip or in addition to the ip all I get is the nginx page in either location. My ALLOWED_HOSTS in settings.py includes the server ip and the domain name. I cannot see any issue in the nginx logs either. Not sure what the issue is at this point.
You should be able to change the server_name to include your domain:
server_name <myserverip> <mydomainname>;
Feel free to drop the ip address if you only want to be able to access the site using your domain name. You'll also want to add any subdomains (i.e. www) you want to serve the same site. For example:
server_name youdomainname.com www.yourdomainname.com;
Don't forget to restart nginx after updating the config file.
In server_name you can write an IP or URL, but at the same time in your settings.py you must give it permission.
[settings.py]
ALLOWED_HOSTS = ['IP', 'URL']
Here, I uploaded something to my github that can help you:
Github/Nginx
I solved the issue. The nginx default configuration /etc/nginx/sites-available/default was shadowing mine. I commented out the server portion of the default configuration and the domain is working as expected.

Defining correctly Nginx server block for two django apps and no server_name

I have been following the Digital Ocean tutorial How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04, so that later i can deploy my own django application using Nginx+uWSGI.
In this tutorial they create 2 basic Django apps to be later served by Nginx. I have tested that the apps were working using the Django server and uWSGI alone.
When i passed to the Nignx part i ran into a problem, basically i dont have a server_name for now only have an IP to work with, and i tried to differentiate between Django apps using the port number.
The default Nginx server (xxx.xxx.xxx.xxx:80) is responding correctly, but when i try to access the Django apps using (xxx.xxx.xxx.xxx:8080 or xxx.xxx.xxx.xxx:8081) i get 502 bad gateway.
I think i have a problem in the way or logic i am defining my listen inside the server block. What would be the correct way of doing this, or what might i be doing incorrectly.
This are my server blocks (in sites-enabled):
firstsite app
server {
listen xxx.xxx.xxx.xxx:8080;
#server_name _;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/firstsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/root/firstsite/firstsite.sock;
}
}
econdsite app
server {
listen xxx.xxx.xxx.xxx:8081;
#server_name _;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/secondsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/root/secondsite/secondsite.sock;
}
}
default Nginx
server {
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
UPDATE:
I was checking the error log under /var/log/nginx and when i try to connect to firstsite i get the following error:
2016/02/05 15:55:23 [crit] 11451#0: *6 connect() to
unix:/root/firstsite/firstsite.sock failed (13: Permission denied)
while connecting to upstream, client: 188.37.180.101, server: ,
request: "GET / HTTP/1.1", upstream:
"uwsgi://unix:/root/firstsite/firstsite.sock:", host:
"178.62.229.183:8080"
Nginx server on ubuntu will run on www-data user by default, uWSGI server won't (which is actually a good thing, unless it runs on root). If you're creating unix socket for uWSGI, access to it will be defined as for any system file. And by default, access to it might be restricted only to user that created socket.
More on that, you're creating your sockets in /root/ directory. That directory is readable only by root user and some of Linux distributions won't allow accessing anything inside even if permissions are set correctly.
So what you have to do is:
put sockets outside of /root/ directory (/var/run is good place for that)
Make sure that nginx will have access to that sockets (put --chmod-socket 666 or `--chown-socket yourusername:www-data into your uWSGI startup line)
And if you're running that uWSGI server on root, be aware that this is really dangerous. Any process running on root can do anything with your system, so if you will make mistake in your code or someone will hack in, he can inject any malicious software into your server, steal some data from it or just destroy everything.