How do I run standalone ruby or python file from nginx? - python

I have a ruby file index.rb that contains puts "this is to show in nginx landing page".
My nginx landing location is /usr/share/nginx/html and my configuration is
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.rb index.html index.htm;
server_name localhost;
}
How do I print those lines without ruby on rails or python django? Do I need to build my code?

Nginx cannot run the index.rb script, in part because it does not have an embedded ruby (or python) interpreter. What you might want to do is to run your script through a uWSGI app. As documented here, for ruby, you need to create an app.ru script with a call entry point (which will just load your index.rb script). It is then run via:
uwsgi --socket 127.0.0.1:3031 --rack app.ru
Your nginx server can then access the uWSGI process with the following configuration:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
uwsgi_modifier1 7;
}

Related

Multiple Django app using nginx and gunicorn on Ubuntu 14.04 trusty server

I am new to server configuration. I do some google and config django app using gunicorn and nginx on ubuntu 14.04 trusty server. For the first django app I use port number 80 and my configfiles are :
/etc/init/gunicorn.conf :-
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock myproject.wsgi:application
My nginx configuration file for first django app:
/etc/nginx/site-available :-
server {
listen 80;
server_name myapp.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock;
}
}
After that, i link site to site-enabled .
Next, i create a new django app inside the first django app virtualenv like:
FirstApp_Virtual_Env\first_djangoapp\app files
FirstApp_Virtual_Env\Second_djangoapp\app files
Now i configure gunicorn for second app like :
/etc/init/gunicorn_t :-
description "Gunicorn application server handling myproject2"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject2
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock myproject2.wsgi:application
My nginx configuration file for second django app:
/etc/nginx/site-available :-
server {
listen 8000;
server_name myapp2.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject2;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock;
}
}
After that i link site to site-enabled .
Now here is my problem: when i type myapp.com then my first django app is working fine but for second app when i type myapp2.com its showing nginx page and when i type myapp2.com:8000 it's working fine . I do some google for that but i am unable to find solution. I am newbie to this so please give me a hint for that how to correct my problem. Thanks for your time.
You configured nginx to serve myapp2.com on port 8000:
server {
listen 8000;
server_name myapp2.com;
# ...
}
so why would you expect nginx to serve it on port 80 ?
[edit] I thought the above was enough to make the problem clear but obviously not, so let's start again:
You configured nginx to serve myapp2.com on port 8000 (the listen 8000; line in your conf, so nginx do what you asked for: it serves myapp2.com on port 8000.
If you want nginx to serve myapp2.com on port 80 (which is the implied default port for http so you don't have to specify it explicitely in your url - IOW 'http://myapp2.com/' is a shortcut for 'http://myapp2.com:80/'), all you have to do is to configure nginx to serve it on port 80 just like you did for 'myapp.com': replace listen 8000; by listen 80;.
If you don't type in a port, your client will automatically use port 80.
Typing myapp2.com is the same as typing myapp2.com:80
But myapp2.com is not running on port 80, it's running on port 8000.
When you go into production it is possible to redirect myapp2.com to port 8000 without explicitly typing it. You register myapp2.com with a DNS name server and point it towards myapp2.com:8000

Nginx reverse proxy on python simplehttpserver

I was trying to setup NGINX reverse proxy on Python SimpleHTTPServer. My web.conf file present in /etc/nginx/conf.d and the setting present in the file is as follows.
server {
server_name localhost;
location / {
proxy_pass http://192.168.1.3:8000/;
}
}
My NGINX is up and running. I did the reload after saving the web.conf file. On the other hand, I'm also running Python SimpleHTTPServer in directory home/user/projects/
But when I open the browser and visit localhost it shows me the NGINX Welcome Page and not the index.html file which is inside the directory in which I'm running Python SimpleHTTPServer.
Two things:
1- you forgot in your configuration file to specify the listening port
just add :
listen 80;
2- the default configuration is still active check if there is symlink called default in:
/etc/nginx/sites-enabled/
and delete it
3- Preferably add your settings file in
/etc/nginx/sites-available/
then make a symlink to it in sites-enabled , that way you can just delete the symlink if you want to deactivate the site instead of removing the configuration . instead of putting it in conf.d.
check how to configure nginx for more details

Can't make Nginx work with uWSGI

I am having this test file called test.py:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
and I run uWSGI using this command:
uwsgi --http :8001 --wsgi-file test.py
I am able to see the "Hello World" in browser by going to xxxx.xxxx.xxxx.xxxx:8001
Now I am trying to add Nginx to this. Here is my file called 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
listen 8000 default_server;
# the domain name it will serve for
server_name _;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/wz/dispatcher/OurAwesomeDjangoApp/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/wz/dispatcher/OurAwesomeDjangoApp/uwsgi_params; # the uwsgi_params file you installed
}
}
When i go to xxxx.xxxx.xxxx.xxxx I see the standard "Welcome to Nginx".
However when I go to xxxx.xxxx.xxxx.xxxx:8000 I am unable to connect.
Django server works on this port python manage.py runserver 0.0.0.0:8000 though.
uwsgi_params is present in my project directory.
I have also created a symlink to /etc/nginx/sites-enabled
What am i missing? Any help appreciated.
EDIT:
When i installed nginx using yum install nginx there were no sites-available and sites-enabled directories in /etc/nginx/.
I created them manually and added:
include /etc/nginx/sites-enabled/*;
to /etc/nginx/nginx.conf

Trouble with using Nginx with django and uwsgi

I follow the steps in http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html but when all the steps done without any error I visit 127.0.0.1:8000, it response with a time-out, my nginx log shows that
upstream timed out (110: Connection timed out) while reading response header from upstream,
By the way, I can access 127.0.0.1:8001 where uwsgi and django works well.
And I can access image in 127.0.0.1:8000/image/1.jpg as well, but just cannot access 127.0.0.1:8000
here's my nginx.conf
upstream django {
server 127.0.0.1:8001;
}
server {
listen 8000;
server_name 127.0.0.1
charset utf-8;
client_max_body_size 75M;
location /media {
alias /home/zhaolei/virtualdjango/bin/mysite/media;
}
location /image {
alias /home/zhaolei/virtualdjango/bin/mysite/image;
}
location / {
uwsgi_pass django;
include /home/zhaolei/virtualdjango/bin/mysite/uwsgi_params;
}
}
I use uwsgi --http 127.0.0.1:8001 --chdir=mysite --module=mysite.wsgi
to run uwsgi. I use uwsgi_params hosts in https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
uWSGI have 2 protocols to communicate with web server. One of them is normal HTTP protocol, that can also be used to communicate directly with clients. But there is also special uwsgi protocol, optimized for communication between HTTP Proxy server and uWSGI.
That protocol is used by nginx when using uwsgi_pass directive, and by uWSGI when you're starting your uWSGI server with --socket param.
If you're starting uWSGI with --http param, uWSGI will use HTTP protocol (that is what you're doing), but if nginx is still using uwsgi_pass it's expecting uWSGI protocol on that socket, not HTTP.
To fix it you have to either change your uwsgi start command to use --socket instead of --http (that's recommended way, but you won't be able to check if uWSGI is functioning properly by entering 127.0.0.8001 directly in your browser, but that's okay: if your command with --http worked properly, there won't be any difference using --socket) or use proxy_pass instead of uwsgi_pass in your nginx config.
And it's described on link that you're mentioned, right here

configuring web.py in nginx.. confusion

Hi I am new with nginx server, and I have uploaded my index.py file at /var/www/pyth/index.py ...
I am a little bit confused because in my local I can run freely
python index.py and access http://127.0.0.1:8080
I was wondering how can I do that in nginx, I have run python index.py but I can't access to mysite.com:8080
this is my config in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;`
#root /usr/share/nginx/html;
#index index.php index.py index.html index.htm;
root /var/www/mysite.com;
index index.php index.py index.html index.htm;
# Make site accessible from http://localhost/
server_name mysite.com;
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
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied reques$
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
...
does anyone has an idea about my case? any help will be appreciated.. thanks in advance
You should set up either a uwsgi (or similar), or a proxy_pass in nginx.
The option with UWSGI is better because it'll use the protocol designed for working with web-servers; though it's a bit harder to set up than just proxying everything via nginx.
proxy_pass
web.py has a web-server just for the development purposes, it shouldn't be used in production environment because it's really slow and inefficient in that case, and using proxy_pass wouldn't be a great idea if you are planning to release it.
With proxy_pass, you leave the 127.0.0.1:8080 server online, and then in nginx (on the same server), set up like that:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The proxy_pass option redirects everything to the web.py server at 127.0.0.1:8080, the other ones - redirect the data about the connection (IP of the connected client and the host that was used for the connection on the nginx's side)
UWSGI
Using UWSGI, in short, is like that:
1) install uwsgi using your distro's package manager, or a pip, or using setup.py install.
2) in nginx, set up a server that will pass everything to the UWSGI server:
server {
listen 80;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9000;
}
}
3) Then, in your web.py application (let's suppose it's called yourappfile.py), instead of app.run(), use:
app = web.application(urls, globals())
application = app.wsgifunc()
You can still have app.run(), just make sure to put it inside the if __name__ == '__main__' block; and make sure the application = app.wsgifunc() is outside so UWSGI could see it.
Then start a UWSGI server:
uwsgi --http :9090 --wsgi-file yourappfile.py
Take a look at these manuals, it may help you:
UWSGI Quickstart
Web.py running on the nginx uwsgi
Deployment of Web.py Applications Using uWSGI and
Nginx
UWSGI Wiki - Examples

Categories

Resources