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
Related
I have a Django App which I want to deploy on a Centos Linux server having a global/public IP which is assigned to a domain and is secured with SSL.
The System configuration is as:
centos-release-6-10.el6.centos.12.3.x86_64
Apache/2.2.15 (CentOS)
When I run the server using:
python manage.py runserver 0.0.0.0:8000,
then it is only accessible from the browser by passing a local IP say
http://192.xxx.xx.xxx:8000/django_app/home
But I want to access it from the public/global IP, but it gives an error when I replace the local IP with Global/Public IP or domain assigned to the public IP as:
105.168.296.58 took too long to respond.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT
When I simply put this public IP in the browser as https://105.168.296.58 then it redirects the browser to the app/website say https://mywebsite.com/home which is running at port 80 on Apache, and shows the assigned domain instead of IP, so IP is working fine.
in settings.py: all hosts are allowed as ALLOWED_HOSTS = ['*']
Methods tried are:
by using django-extensions as:
python manage.py runserver_plus --cert certname 0.0.0.0:8000
by using django-sslserver as:
python manage.py runsslserver 0.0.0.0:8000
The app runs from both of these methods only locally at http://192.xxx.xx.xxx:8000/django_app/home, but None of them worked from a Public IP.
Kindly tell, How to deploy or configure this Django App to Run Outside the Local Network?
i would deploy django like this:
install uwsgi in your virtualenv
pip install uwsgi
create a uwsgi config file (everything inside <> has to be edited to your needs
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = </path/to/your/project>
# Django's wsgi file
module = <project>.wsgi
# the virtualenv (full path)
home = </path/to/virtualenv>
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = </path/to/your/project/mysite.sock>
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
install nginx and create following config
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///<path/to/your/mysite/mysite.sock>;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name <example.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 </path/to/your/mysite/media;> # your Django project's media files - amend as required
}
location /static {
alias </path/to/your/mysite/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 </path/to/your/mysite/uwsgi_params;> # the uwsgi_params file you installed
}
}
Download and copy uwsgi_params to your directory
https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
symlink config from /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
Start uwsgi and nginx
uwsgi --ini mysite_uwsgi.ini
sudo service nginx restart
don't forget collectstatic so that these files can be served.
we added the mysite_uwsgi.ini and uwsgi_params files to our repository
I have created a flask application and deployed it in a remote ubuntu server using Nginx and uwsgi. But I can't access images in the application folder. The images are stored in /myproject/static/images/ folder.
Here is the server block
server {
listen 80;
server_name _;
location / {
include uwsgi_params;
uwsgi_pass unix:/root/myproject_sock/myproject.sock;
}
}
How can I access the images kept in /myproject/static/images/ folder through image URL i.e. HTTP://myip/myproject/static/images/first_image.jpg ?
Thanks to #RichardSmith (https://stackoverflow.com/users/4862445/richard-smith) the issue is solved.
If you are facing the same problem, you need to edit your server block inside /etc/nginx/sites-available/myproject (myproject can be anything you named your project earlier).
You need to add location and root directive. for example: location /DIR_NAME/myproject/static/images/ { root /; }. Here inside DIR_NAME you need to put the parent directory of your myproject folder.
Follow: http://nginx.org/en/docs/beginners_guide.html
I'm started developing a new site using Django. For realistic testing I wanted to run it on a Synology DS212J NAS.
Following the official Synology guides I installed ipkg and with it the mod_wsgi package.
As Next step: Following the standard tutorial I made a virtualenv and installed Django in it. Opening a new project and adjust the settings following to: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04
I'm able to reach the "Hello World" Site from Django by use of manage.py
As suggested I want to exchange the manage.py through the apache server on the NAS. So I think I should go and edit the apache config files for e.g. define a virtual host...
However I can't localize the files for it, as it seems they where moved at DSM6 (which I use) in comparison too other guides.
Where need I to enter the values following the Tutorial on the Synology?
As I'm quite new into the topic do I need to especially load the mod_wsgi module for Apache and if where?
Is it a good idea to use the basic mode of wsgi instead of the daemon mode? I'm not sure which Django modules will be used later on in development...
Thanks for the support!
Activate the python 3 package and the webstation
In webstation> general settings> main server http enable nginx
In Control Panel> Network> DSM Settings> Enable Custom Domain: "test"
(which will allow us to access the nas by entering test.local and simplify the task later.)
Enable ssh connection in control panel> terminal and smtp
We use the ddns service of synology to have external access in our case "test.synology.me"
In control panel> security> certificate : we generate our ssl certificate with let's encrypt
Connect to the nas in ssh
Take root rights sudo -i
Install virtualenv: easy_install virtualenv
We set up our virtual environment: virtualenv -p python3 flasktest
Flask and gunicorn are installed:
pip install flask gunicorn
We create our web application, file: init.py
We launch our web application with gunicorn:
gunicorn --certfile /usr/syno/etc/certificate/system/default/cert.pem --keyfile /usr/syno/etc/certificate/system/default/privkey.pem -b 127.0 .0.1: 5000 app: app
In /etc/nginx/sites-enabled we create a server configuration file, we will use nginx as a proxy, in our case the file will be flasktest.conf
flasktest.conf file:
`
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
gzip on;
server_name test.synology.me;
location / {
proxy_pass https://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_log /volume1/projects/flasktest/logs/error.log;
access_log /volume1/projects/flasktest/logs/acess.log;
}
`
Open the control panel port> external access> router configuration> create> integrate application> enable the check box for webstation and apply
We check our server file for that we enter the command, nginx -t
We are restarting nginx synoservicecfg --restart nginx
You now have access to your python web applications from outside in https ** https: //test.synology.me**
a little more information ...
To end and access your application permanently if you will ever be able to reboot, crash ... you can create a script that will restart gunicorn because otherwise the webstation takes over elsewhere if you enter ip nas locally you do not will not see your web apps in python because we did not modify the main configuration file /etc/nginx/nginx.conf locally so this is the default index.html page of the webstation that will be displayed.
example:
cd / volume1 / projects / flasktest
source bin / activate
gunicorn --certfile /usr/syno/etc/certificate/system/default/cert.pem --keyfile /usr/syno/etc/certificate/system/default/privkey.pem -b 127.0.0.1:5000 app: app
</ dev / null 2> & 1 &
This method found with other python framework
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
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;
}