I would like to host 2 sites in one IP address 1.2.3.4 for example. I want to visit them by using different ports. For example, I would like to have 1.2.3.4:8000 for siteA, while 1.2.3.4:9000 to point to siteB. I am using nginx + uwsgi.
Here is the example to configure one of sites.
For NGINX, I had:
server {
listen 8000; ## listen for ipv4; this line is default and implied
location / {
uwsgi_pass unix:///tmp/uwsgi.sock;
include uwsgi_params;
uwsgi_read_timeout 1800;
}
}
For UWSGI, I had:
[uwsgi]
socket = /tmp/uwsgi.sock
master = true
harakiri = 60
listen = 5000
limit-as = 512
reload-on-as = 500
reload-on-rss = 500
pidfile = /tmp/uwsgi.pid
daemonize = /tmp/uwsgi.log
**chdir = /home/siteA**
module = wsgi_app
plugins = python
To visit siteA, I simple go to 1.2.3.4:8000.
I have no problem with configuration of one site, but I have no idea to make it working with two sites.
Please note that I didnot bind the site with the server name. Does it matter?
Thanks in advance.
P.S. The following is the way I launch NGINX and UWSGI.
I first put the nginx conf file (for siteA, I called it as siteA_for_ngxing.conf) in the /etc/nginx/sites-available/ directory.
I then use uwsgi --ini uwsgi.ini to start uwsgi. (the file of uwsgi.ini contains the above [uwsgi])...
Any help?
The following example might be useless for you, because it seems you installed uWSGI manually, instead of using system repository. But I think, you can easly find how uWSGI is configured on Ubuntu and make the same configuration on your system.
Here how I have done it on Ubuntu. I installed both uWSGI and nginx from Ubuntu repo, so I got the following dirs:
/etc/nginx/sites-available
/etc/nginx/sites-enabled
/etc/uwsgi/apps-available
/etc/uwsgi/apps-enabled
On /etc/uwsgi/apps-available I placed two files: app_a.ini and app_b.ini. There is no option socket (as well as pid and daemonize) in these files. uWSGI will detect socket, log, and pid file names using ini-file name. Then I created symlink to these files in /etc/uwsgi/apps-enabled to enable apps.
For nginx I used /etc/nginx/sites-available/default config file (it already symlinked to enabled dir).
upstream app_a {
server unix:///run/uwsgi/app/app_a/socket;
}
upstream app_b {
server unix:///run/uwsgi/app/app_b/socket;
}
server {
listen 8000;
location / {
uwsgi_pass app_a;
include uwsgi_params;
}
}
server {
listen 9000;
location / {
uwsgi_pass app_b;
include uwsgi_params;
}
}
Related
I have previously configured an app in the exact same way but when I updated Ubuntu, I broke my python install. I fixed python, and tried to set up another app in the exact same way, using the same tutorials, but I am getting a 111 error when I try to connect. I can connect if I run the app using gunicorn (which I only tried because it wasn't working using nginx), but I want to find the problem and fix it. As far as I can see everything is configured according to the documentation I went through.
Here is the uwsgi config file
# glossbox_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /usr/local/apps/glossbox/glossbox
# Django's wsgi file
wsgi-file = /usr/local/apps/glossbox/glossbox/glossbox/wsgi.py
# the virtualenv (full path)
home = /usr/local/apps/glossbox/venv
plugins = python
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = server 127.0.0.1:8001
# ... with appropriate permissions - may be needed
chmod-socket = 664
clear environment on exit
vacuum = true
here is the nginx conf file:
# glossbox_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
listen 80;
# the domain name it will serve for
server_name vps746196.ovh.net; # 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 /usr/local/apps/glossbox/glossbox/media; # your Django project's media files - amend as required
}
location /static {
alias /usr/local/apps/glossbox/glossbox/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 /usr/local/apps/glossbox/glossbox/uwsgi_params; # the uwsgi_params file you installed
}
}
the app runs using the command 'uwsgi --ini glossbox_uwsgi.ini' but I get 111 connection refused when I try to connect.
I tried to run using 'gunicorn glossbox.wsgi:application --bind 0.0.0.0:8000' just to see if it worked, and it did, although site couldn't find the static files for the admin site, I assume that is down to me having no configuration set up for gunicorn. Either way, I need to find out why it is refusing the connection.
Any help will be appreciated :-)
I am developing python programs and I have a flask file which runs them. Now I am trying to run flask automatically from Nginx and linking them with uWSGI. It's not working and getting stuck.
I followed this page.here is the link: https://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/
I have performed all the necessary steps and got 502 bad gateway error.
while executing
uswgi --ini /var/www/tg/tg_uswgi.ini
I am getting the following
[uSWGI] getting INI configuration from /var/www/tg/tg_uwsgi.ini
after this, I am not getting anything.when i run my server, it still gives me 502 Bad gateway.
this is my
tg_nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location / { try_files $uri #yourapplication; }
location #yourapplication {
include uwsgi_params;
uwsgi_pass unix:/var/www/tg/tg_uwsgi.sock;
}
}
this is my tg_uwsgi.ini
[uwsgi]
#application's base folder
base = /var/www/tg
#python module to import
app = fileforflk //fileforflk is my flask file which calls other python
//files
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file's location
socket = /var/www/demoapp/%n.sock
#permissions for the socket file
chmod-socket = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log
this is my flask file
from flask import Flask
import browser //python file the flask is calling
app = Flask(_name_)
#app.route('/test',methods= ['GET'])
def result():
return ("success")
if _name_ == '_main_':
app.run(host = '0.0.0.0',port=5000)
After getting INI configuration, the terminal is not showing anything and the server still returns bad gateway. please help me with this.
You should define:
socket = /var/www/demoapp/%n.sock
And :
uwsgi_pass unix:/var/www/tg/tg_uwsgi.sock;
should be matching. So for example, define:
socket = /var/www/tg/%n.sock
And :
sudo chown -R www-data:www-data /var/www/tg/
sudo chown -R www-data:www-data /var/log/uwsgi/
nginx config for the server (the main nginx one is the default one on debian 9):
server {
listen 80;
server_name subdomain.domain.com;
include /etc/nginx/mime.types;
location /galleries {
autoindex on;
alias /srv/galleries/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/scraper.sock;
}
}
uwsgi config:
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = /tmp/scraper.sock
chmod-socket = 777
uid = www-data
gid = www-data
vacuum = true
die-on-term = true
plugins = python3
py-autoreload = 1
If I try creating a route for /galleries/whatever, ie like this:
#app.route("/galleries/whatever")
def test():
return "Hello"
I'll just see the indexed files inside /galleries/whatever through nginx instead of going through flask.
Is there a way for me to force nginx to only handle requests if flask returns 404? Alternatively, is there a better way for me to serve files while still having them available under those urls? Keep in mind the /galleries folder is pretty big and generated by another program.
I run the server with "uwsgi --ini server.ini" and nothing else.
This is really weird situation.
I'm trying to create an web framework using Python from scratch.
And I'm trying to get the current working directory (technically the full path of the current .py file) of a file using below code.
os.getcwd()
The problem is..
If I type above code and restart uwsgi server then print it.
At the first time, it always prints like below.
/var/www/html/mydocroot
Then if I refresh the page, 'mydocroot' directory disappears automatically like below.
/var/www/html
And it creates same output after refreshing the page as above.
I spent almost whole day to figure out that issue.
Any idea or same experiences?
and also I tried,
os.path.dirname(os.path.abspath(__file__))
this one as well.
For example,
/var/www/html/mydocroot/Core/Settings
is the result when I restart uwsgi server,
/var/www/html/Core/Settings
When I refresh the page, 'mydocroot' disappears by itself as above.
server{
server_name example.com;
rewrite ^(.*) http://www.example.com permanent;
}
server {
listen 80;
server_name example.com *.example.com;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/html/example/myapp.sock;
}
}
Above is my nginx server config. and below is wsgi configuration.
[uwsgi]
module = wsgi:application
master = true
processes = 1
touch-reload = /var/www/html/ggr/wsgi.py
socket = myapp.sock
chmod-socket = 664
vacuum = true
max-requests = 1
die-on-term = true
logger = file:/var/log/ggr.log
catch-exceptions = true
Files don't have a "current working directory". There's just one global current working directory for a given process. The current working directory can change
os.chdir('/new/working/directory')
If you want the directory of a specific file, you can do this
os.path.dirname(os.path.abspath(__file__))
I am having a bit of trouble getting Nginx to serve a static index.html page, and also to serve a a Django site. Could anyone point me out to what I'm doing wrong?
I have port 80 and 8000 open.
I have spent three days trying to get this working.
Locally I have no problems, then again I'm not using nginx for that.
I placed uwsgi_params within /etc/nginx/uwsgi_params
/etc/nginx/sites-enabled
Here are my symlinks to the actual configuration files
site1.conf -> /home/jesse/projects/site1/conf/site1.conf
site2.conf -> /home/jesse/projects/site2/conf/site2.conf
/home/jesse/projects/site1/conf/site1.conf
This is just a basic static site, but it won't load :(
server {
listen 80;
server_name www.site1.com;
rewrite ^(.*) http://site1.com$1 permanent;
location / {
root /home/jesse/projects/site1/;
}
}
/home/jesse/projects/site2/conf/site2.conf
= The manage.py/wsgy.py is located under /home/jesse/projects/site2/site2/
= This is a Django site using uWSGI, I installed it with $ pip install uwsgi.
server {
listen 80;
server_name www.site2.com;
rewrite ^(.*) http://site2com$1 permanent;
root /home/site2/projects/site2/site2;
location /static/ {
alias /home/jesse/site2/projects/site2/site2/static/;
#expires 30d;
}
location /media/ {
alias /home/jesse/site2/projects/site2/site2/media/;
#expires 30d;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
/home/site2/projects/site2/conf
[uwsgi]
projectname = site2
projectdomain = site2.com
base = /home/jesse/site2/projects/site2/site2
# Config
plugins = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/src/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8000
logto = %(base)/logs/uwsgi.log
# Runs daemon in background
daemonize = /home/jesse/log/$(projectname).log
Nginx Restart
$ sudo service nginx restart
* Restarting nginx nginx [ OK ]
= The site1 produces a Not Found (Not a 404)
= The site2 produces a
I would appreciate any assistance :)
rewrite ^(.*) http://site1.com$1 permanent;
this in site1, is not managed by any server, because no server_name handles site.com (without www).
then again with site2
rewrite ^(.*) http://site2com$1 permanent;
first fix those lines.
the correct way in my opinion is to write a server rule that catches www. names and rewrites them to non www, and place site1.com or site2.com as server_name in the rules you have now, as an example of rewiring
server {
listen 80;
server_name www.site1.com
return 301 http://site1.com$request_uri?;
}