I'm very new to NGINX, or setting up a server in general. I'm going through the tutorial http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html with a website I'm trying to set up. I'm getting to the point in the tutorial where you use sockets, however, I can't get the socket to work. nginx.conf looks like this.
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/althor/projects/Freebooks/freebooks.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 10.0.0.130; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/althor/projects/FreeBooks/media; # your Django project's media files - amend as required
}
location /static {
alias /home/althor/projects/FreeBooks/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/althor/projects/FreeBooks/uwsgi_params; # the uwsgi_params file you installed
}
}
The command i'm doing is this:
uwsgi --socket freebooks.sock --module FreeBooks.wsgi --chmod-socket=664
My File structure looks like this
-home
-projects
-FreeBooks
freebooks_nginx.conf
freebooks.sock=
-FreeBooks
wsgi
I'm not getting any type of error. When I go to 10.0.0.130, all I get is the welcome to nginx thing, same for 0.0.0.0, 127.0.0.1. Except when I got to port 8000 where I get a bad nginx gateway. Any help would be very much appreciated.
remove your "upstream django" line and replace your uwsgi_pass line with
uwsgi_pass unix:///home/althor/projects/Freebooks/freebooks.sock;
so that your file looks like
server {
listen 8000;
server_name 10.0.0.130; # substitute your machine's IP address or FQDN
charset utf-8;
client_max_body_size 75M; # adjust to taste
location /media {
alias /home/althor/projects/FreeBooks/media;
}
location /static {
alias /home/althor/projects/FreeBooks/static;
}
location / {
include /home/althor/projects/FreeBooks/uwsgi_params;
uwsgi_pass unix:///home/althor/projects/Freebooks/freebooks.sock;
}
}
Also make sure the socket file actually gets created when you run uwsgi command and that it is readable by the user running nginx (the properties are right but does the user have access to the full path?)
It turned out be a permissions issue. NGINX didn't have permission to write to the socket.
Related
I am trying to do the following.
I have 3 django projects (NOT apps) (can be more).
Proj1: On port 8000, Proj2: 8001, and Proj3:8002
This is what I am trying to achieve:
User visits : localhost:8000
All urls under Pr1: Respond
User visits: localhost:8000/Pr2
All urls under Pr2: Respond
User visits: localhost:8000/Pr3
All urls under Pr3: Respond
Goal: Proj 1 is run with Docker and Nginx. Now I have 2 new projects to integrate. Every new sub project should be reached using only one main port (8000). But internally the request is directed towards the correct port of sub-project.
Why this goal: Achieve micro-service style structure. Sub projects (2 & 3) can be reached only after authentication through Proj1.
What have I tried: Honestly no clear idea where to start.
Added new projects to docker-compose...they all work when visited by their own port(localhost:8001/Prj2) ..
With nginx, tried adding a new server. No effect:
server {
listen 8001;
location / {
proxy_pass http://web_socket:8080;
proxy_redirect off;
}
}
If you have "nginx" installed, so you can just create two files (site1.com.conf, site2.com.conf) in the following directory: "/etc/nginx/sites-enabled/"
Then add inside this files codes below:
site1.com.conf
# the upstream component nginx needs to connect to
upstream site1 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:49151; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name site1.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/site1.com/src/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/site1.com/src/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass site1;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
site2.com.conf
# the upstream component nginx needs to connect to
upstream site2 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:49152; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name site2.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/site2.com/src/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/site2.com/src/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass site2;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
Then edit "/etc/hosts" and add there two lines:
127.0.0.1 site1.com
127.0.0.1 site2.com
After saving files, restart uwsgi (install if it is not installed) and **nginx**
**service uwsgi restart**
**sudo /etc/init.d/nginx restart**
**Note: all your django applications should be in the folder of "/var/www/site1.com/src" and "/var/www/site2.com/src"**
I have a single application in django hosted on AWS. But, in these days, I turned It into multi-tenancy, using django-tenant-schemas. Locally, It runs normally. I can create my tenants and access them on my local django server. However, I'm not getting to run on AWS.
My .conf file for NGINX looks like this:
upstream django {
server unix:///home/ubuntu/folder_my_projct/mysite.sock; # for a file s$
}
# configuration of the server
server {
listen 80; address or $
server_name ssh *.example.com.br;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /media {
alias /home/ubuntu/folder_my_projct/media; # your Django project's$
}
location /static {
alias /home/ubuntu/folder_my_projct/static; # your Django project's$
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/folder_my_projct/uwsgi_params; # the uwsgi$
}
}
I did pull for my project that runs on AWS, everything worked in my Virtual Machine (makemigrations and migrate_schemas). However, when I try to access the subdomain, It doesn't work. My only change to access the subdomains was in the file above, pasting the * before the dot: * .example.com.br. I tried using regex, but it didn't work as too (server_name ~^(?<subdomain>.+)\.example\.com\.br $;). I do really appreciate if someone tells me what I'm doing wrong, or if I need to do anything else.
Change this line server_name ssh *.example.com.br;
by
server_name ssh.example.com.br *.example.com.br;
or server_name *.example.com.br;
For each subdomain you'll need to create a configuration, depending what you want to access
server {
server_name ssh.example.com.br;
...
}
server {
server_name blabla.example.com.br;
...
}
server {
server_name *.example.com.br;
...
}
I've been working through a turotial and got stuck at this specific part and can't seem to figure it out. This is exactly where I am stuck.
Here is my mysite_nginx.conf
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
root / ;
listen 8000;
# the domain name it will serve for
server_name .81.*.*.*; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media/ {
root /home/mysite/mysite1/blog/media; # your Django project's static files - amend as required
}
# Django static
location /static/ {
#alias /home/mysite/mysite1/blog/templates; # your Django project's media files - amend as required
root /home/mysite/mysite1/blog/static;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/mysite/mysite1/uwsgi_params; # the uwsgi_params file you installed
}
}
Here is the file structure of mysite on pastebin - Sorry for it being a total mess. I am aware of redundant static files. If you need any other files, I am happy to provide. Thank you.
This situation is some kind of hard to describe. I use nginx and uwsgi to deploy my Django blog on my VPS, it was function well, but I can only use my IP to get access to the app, how can I use my domain without www to setup my app? I set the server_name breakwire.me and set a A record HOST is '#', point to my VPS's IP, but if I visit the breakwire.me, I can only see
"Welcome to nginx"
Here is my nginx conf
# my_blog__nginx.conf
# configuration of the server
server {
the port your site will be served on
listen 106.186.29.228:8000;
# the domain name it will serve for
server_name breakwire.me; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# logs
access_log /home/chen/DjangoProjects/my_blog/logs/access.log;
error_log /home/chen/DjangoProjects/my_blog/logs/error.log;
# Django media
location /media {
alias /home/chen/DjangoProjects/my_blog/media; # your Django project's media files - amend as required
}
location /static {
alias /home/chen/DjangoProjects/my_blog/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 106.186.29.228:8001;
include /home/chen/DjangoProjects/my_blog/uwsgi_params; # the uwsgi_params file you installed
}
}
Change the
listen 106.186.29.228:8000
to
listen 106.186.29.228
or: 106.186.29.228:80
The browsers are always connecting on port 80. Your virtual server in nginx is listening on port 8000, so if you try to open your website with http://breakwire.me:8000/
it's working because you've set the port to 8000 in the listen directive.
I need to deploy my django site using ngnix and uwsgi. it works fine in localhost. but when I give my remote server, it does not work. this is my ngnix.conf file
# nginx.conf
upstream django {
# connect to this socket
server unix:///tmp/uwsgi.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket
}
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 192.168.101.191; # substitute your machine's IP address or FQDN
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/calcey/django_env_2/employee/media; # your Django project's media files
}
location /static {
alias /home/calcey/django_env_2/employee/static; # your Django project's static files
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # or the uwsgi_params you installed manually
}
}
"192.168.101.191" means my server IP address. if I put local host instead of "192.168.101.191" it works. but for remote IP it does not work. please give me a suggestion.
Your server IP address looks some kind of Internal IP. But anyway, you do Not need any Server IP address as long as your Nginx script is running on the Server.
You can use the following configurations Instead:
Server {
listen 80;
server_name 127.0.0.1;
....
....
....
}
Hopefully, this will solve your problem.