Django & Nginx issue with "www" and non-"www" URL - django

I have a problem understanding how to run my Django app with Nginx (server is Ubuntu).
So, I have a site which allows for user accounts, and it currently has a problem, where both "www" and non-"www" URLs load. This is problematic, because logging-in to one, will not reflect to the other. Besides that, it's also confusing. I want to keep the non-"www" variant.
I followed the instructions here on how to do it using Ngnix:
https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04
And it worked when I tried to load the domain. But when I started the Django app, it stopped working.
I'm really confused on how to get it working, is there some way of running my django app through Nginx?
Thanks!
EDIT: I've included the server config thing (I think)
default
server {
server_name www.nilly.com;
return 301 $scheme://nilly.com$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}

Related

Serving React and Django with SSL and a domain

Currently, I'm deploying a react app and using django as the backend API on an ubuntu nginx server. The react app is already online have a SSL certificate, but the backend API does not. By default, browsers can't show a http content on a https connection.
Do I need to get another SSL certificate afor the backend API? Or is there another way to do it?
Nginx conf file (for the frontend part. I'm not sure how to configure the backend):
The backend is currently running on xxx.xx.x.xx:8000 (using gunicorn --daemon --bind xxx.xx.x.xx:8000)
server {
server_name xxxxxx.com www.xxxxxx.com xxx.xx.x.xx;
root /var/www/frontend/build;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/fromnil.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/fromnil.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = www.xxxxxx.com) {
return 301 https://$host$request_uri;
}
if ($host = xxxxxx.com) {
return 301 https://$host$request_uri;
}
server_name xxxxxx.com www.xxxxxx.com xxx.xx.x.xx;
listen 80;
return 404;
}
Thanks
Found this link, but wasn't able to post a comment because my reputation is not enough. And I don't really understand. Can anyone help me?
How to deploy react and django in aws with a ssl and a domain

Need help on connecting django to react using nginx and gunicorn

I'm trying to connect Django back-end to a React build provided to me by the front-end developer. I'm using Gunicorn for Django and Web server is Nginx.
The below config file is a result of extensive Googling.
Currently Django back-end works on port 80/8000 but whenever I change the port to anything else like 8001 below, the server does not respond.
The complete thing is running on Google Ubuntu VM.
I've executed sudo ufw disable for testing purposes.
server {
#listen 80;
listen 8001;
listen [::]:8001;
server_name xx.xx.7.xx;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /static/ {
root /home/username/cateringm;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
#location / {
# try_files $uri $uri/cm_react_build/build/index.html; # this is where you serve the React build
# }
}
server {
listen 8002;
listen [::]:8002;
server_name xx.xx.7.xx;
root /home/username/cm_react_build/build;
index index.html index.htm;
location /static/ {
root /home/username/cm_react_build/build;
}
location /test {
root /home/username/cm_react_build/build;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}
I'm new to configuring web servers. Help would be appreciated.
I found the problem. Google by default blocks all ports except port 80. I updated the firewall rules for different ports and the requests were going through.

ELB transferring to wordpress with Nginx on EC2 shows 404

※ I'm using example.com as example
I'm developing an app with ELB and EC2.
The structure is as followed.
When an user access to /manage/*, ELB will transfer to wordpress hosted instance.
Here is the ELB setting.
About ELB it works, but after transfered to wordpress side, CSS and JS files' response show 404.
When I access to /magazine/* URL, loading animation of wordpress theme starts to work but get stuck because of not loaded css and js.
I'm guessing it's because of nginx configuration but can't solve it.
This is the content of configuration file. I just add /etc/nginx/conf.d/vhosts.conf and write some settings. I don't touch other files.
# vhosts.conf
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name example.com;
root /var/www/html;
location /magazine {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
below /var/www/html, project's structure is as followed.
/wp-content
/wp-admin
/wp-include
index.php
wp-config.php
for other information,
I installed php-fpm.
SSL certification is set up with ACM and Route53
Additional Info
Both siteurl and home in DB table wp_options are set to https://example.com/magazine.
Result
made /var/www/html/magazine directory and move all files into it.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name example.com;
location ~* /(wp-config|xmlrpc)\.php {
deny all;
}
location #magazine {
rewrite ^/magazine(.*) /magazine/index.php?$args;
}
location ^~ /magazine {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ #magazine;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
}
Since you are not serving wordpress from the root of your url, you must tell wordpress to load the assets from the subdirectory. How you can do this, is described in a wordpress support article. [1]
Did you check whether the WP_HOME and WP_SITEURL variables are set correctly?
Could you edit your question and include the URL your browser tries to load for the css and js files since it is not fully visible in the screenshot?
Edit:
Thanks to the additional information we know that wordpress constructs the path as expected. So, now I share your opinion that nginx is causing the issue.
I looked around and noticed two possible issues with the nginx configuration:
The root statement should probably be inside the location block. I do not know if this is really necessary, but many people do it. [2]
The location block does a prefix match and includes the prefix magazine into the $uri variable as stated in a serverfault thread. [3]
You could strip away the prefix by using a regex as follows:
location ~ ^/magazine(.*) {
root /var/www/html;
try_files $1 $1/ /index.php?$args;
}
Note: I do not know if the directory redirect $1 and default redirect to /index.php?$args make sense for serving js/css assets. You should probably remove them if they are not necessary.
References
[1] https://wordpress.org/support/article/changing-the-site-url/#changing-the-site-url
[2] https://serverfault.com/a/258540/531099
[3] https://serverfault.com/a/455853/531099

Getting a 404 on Nginx RTMP Module stats file

I'm not understanding why I get a 404 when I set up my stats view in Nginx RTMP Module.
The instructions I'm following are fairly straightforward and especially here.
I'm using a Ubuntu VM on Google Cloud.
I've placed my stat.xsl file in /var/www/html
That is the default location for Nginx assets, as specified in sites-enabled/default folder:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
And here is the snippet I copy/pasted and adapted in my nginx.conf:
server{
listen 80;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
# under linux you could use /var/user/www for example
root html;
}
}
The goal here is to be able to type [my-ip-address/stat and get the formatted xsl content.
There's something here I'm not getting -any help please?
Have you tried typing /statOR instead?
server {
listen 80;
server_name localhost;
# rtmp statistics
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
# under linux you could use /var/user/www for example
root html;

How do I point two domains to the same django page?

I have a Django app running a site, abc.com using nginx on the frontend.
The URL: http://abc.com/products/widget shows the widget product page. I would like to also have the URL http://getwidget.com point at the same product page. How can I configure nginx to do this?
My current nginx config is like this:
server {
listen 80;
server_name abc.com;
location / {
client_max_body_size 10M;
proxy_pass http://127.0.0.1:8200;
}
location /static/
{
root /home/projects/abc/static;
}
}
Add another server {} something like this:
server{
listen 80;
server_name getwidget.com;
root /path/to/webroot;
}
This new server can point to your project as well as the old one. If you hardcoded some urls in your project you will need to correct this.