I m very new in flask, try to run a web application with backend as flask. My project folder structure is
~myproject/
~myproject/app (flask api)
~myproject/web (index.html)
running using uwsgi and nginx
uwsgi.ini
[uwsgi]
vhost = true
socket = /tmp/flask.sock
venv = /flask_app/.env
chdir = /flask_app/app
module = app
callable = app
nginx.conf
upstream flask_server {
ip_hash;
server 0.0.0.0;
}
server {
listen 80;
server_tokens off;
server_name _;
root /flask_app/web;
index index.html index.htm index.nginx-debian.html;
charset utf-8;
client_max_body_size 75M;
location / {
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass unix:/tmp/flask.sock;
}
#location /static {
# alias /flask_app/static;
#}
location /flask/ {
proxy_redirect off;
proxy_pass http://flask_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
But index.html is not loading , it is only allowing api url (like: 127.0.0.1/login)?
This directive tells Nginx just to send everything matching that location block to the upstream server uwsgi_pass unix:/tmp/flask.sock
If you want to serve just index.html and pass everything else upstream then you could change it like this:
location / {
try_files $uri/index.html #upstream;
}
location #upstream {
include uwsgi_params;
uwsgi_pass unix:/tmp/flask.sock;
}
Related
i have a config for my django app deployment with nginx
and want to cache some of data
server {
listen 80 default_server;
listen [::]:80 default_server;
root /app/;
location / {
proxy_pass http://landing_page;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /app/staticfiles/;
try_files $uri $uri/ ;
}
location /media/ {
alias /app/media/;
try_files $uri $uri/ ;
}
location ~* \.(svg|ico|woff|woff2|ttf)$ {
expires 1M;
add_header Cache-Control "public";
}}
when i remove cache control from config , nginx work properly and serve data like *.svg but when i enable it cached file wont serve
I am using nginx to serve a go web app running on an Ubuntu 18.04 server. I want it to respond to the index directive first when I go to mydomain.com but in the nginx config I have below it is going straight to the web app and ignoring my index.html file. It does work if I get mydomain.com/index.html but won't get that file from index. /var/log/nginx/error.log has no errors.
The static files are chown'ed to root:www-data with chmod 640.
My static files are at /srv/static/ and my go web server is at /srv/web/
Here is my sites-available config file:
server {
listen 80;
server_name mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
server_name mydomain.com;
listen 443 ssl;
ssl on;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
root /srv/static;
index index.html;
location / {
try_files $uri #proxy;
}
location #proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8000;
}
}
location / {
try_files $uri $uri/ #proxy; # add $uri/ here
}
I have nginx configuration file which has to server example.com and www.example.com.
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
auth_basic "example Login";
auth_basic_user_file /etc/nginx/.htpasswd;
root /projects/www/example;
index index.html;
location ~* \.(html|js|jpg|png|gif|css|perfumes|imgs|map|fonts|otf)$ {
index index.do index.html index.htm;
access_log off;
}
location /.protected {
access_log off;
auth_basic off;
}
location /health {
access_log off;
auth_basic off;
}
location / {
try_files $uri $uri/index.html;
}
location /hello {
proxy_pass http://localhost:8282;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
I am trying to protect this domain for now, cuz we are preparing the launch. But there is only link which should let the requests pass through without password, if a link has '.protected' in it.
For example,
www.example.com/.protected/file1.txt should be allowed without entering a password.
It is working ok, but the issue is example.com/.protected/file1.txt (without 'www').
If I type only domain name 'example.com' (without 'www'), it automatically redirects to www.example.com as configured, but 'example.com/.protected/file1.txt' doesn't redirect to 'www.example.com/.protected/file1.txt'. It seems if the domain name has some paths, the domain name (without 'www') doesn't redirect to 'www.example.com'
I am getting 'curl: (6) Could not resolve host:'
Is there anything wrong with my configuration file?
I used the instructions on the following link:
"Hosting Clojure Web Apps in 7 Easy Steps"
I know the uberjar works because i tested it both on my dev machine and the VPS.
It's just that Nginx doesn't seem to be able to find it.
I suspect that it has something to do with this site code:
# Web sockets
location /chsk {
proxy_pass http://backend/chsk;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...but I don't know how to correct it...thanks for the help!
One other thing: on the "upstream backend" in the site file i tried both 127.0.0.1:3000 AND 0.0.0.0:3000 with no success.
Here's the default site config:
server {
# Replace this port with the right one for your requirements
listen [::]:80 ipv6only=off;
# Multiple hostnames separated by spaces. Replace these as well.
server_name clmitchell.net www.clmitchell.net main.clmitchell.net
books.clmitchell.net dna.clmitchell.net help.clmitchell.net
history.clmitchell.net svcs.clmitchell.net;
server_name_in_redirect off;
root /data/nginx/www/$host;
error_page 401 /error/401.shtml;
error_page 402 /error/402.shtml;
error_page 403 /error/403.shtml;
error_page 404 /error/404.shtml;
error_page 500 501 502 503 504 /error/500.shtml;
location ^~ /error/ {
internal;
root /data/nginx/www/www.clmitchell.net;
}
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/error.log;
index index.php index.html index.htm default.html default.htm;
# Support Clean (aka Search Engine Friendly) URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.scm$ {
include fastcgi_params;
fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need to
fastcgi_pass 127.0.0.1:9981;
}
location ~ /\.ht {
deny all;
}
}
I removed history.clmitchell.net from the list of server names.
Here's the current history site config:
upstream backend {
server 104.131.29.212:3000 fail_timeout=0;
}
server{
listen [::]:80 ipv6only=off;
server_name localhost history.clmitchell.net;
access_log /var/log/hist_access.log;
error_log /var/log/hist_error.log;
root /var//resources/public;
# Web sockets
location /chsk {
proxy_pass http://backend/chsk;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Static assets
location / {
try_files $uri #backend;
}
# The backend server
location #backend {
proxy_pass http://backend;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
there was a duplicate "listen" directive on the history site config, which i removed...but for some reason I'm still getting the error: '
sudo nginx -t
nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/hist:6
nginx: configuration file /etc/nginx/nginx.conf test failed
Please try
proxy_pass http://backend;
And make sure you can access http://127.0.0.1:3000/chsk if your upstream is defined as below
upstream backend {
server 127.0.0.1:3000;
}
Or if we has only one backend server we can just use proxy_pass without upstream backend defined. e.g.
proxy_pass http://127.0.0.1:3000;
I learned a new lesson today: no two sites on a Nginx web server can have the same listen port!
I moved the new site to a new port and updated all the links...PROBLEM SOLVED!
I'm attempting to have nginx reverse proxy static files from an application if the application is serving them, else serve them itself. Currently, I have this configuration:
upstream app_server {
server unix:/tmp/gunicorn.sock fail_timeout=0;
}
server {
listen 8080;
server_name example.com;
access_log /var/log/nginx.access.log;
error_log /var/log/nginx.error.log;
keepalive_timeout 5;
location /static {
try_files $uri #proxy_to_app;
alias /path/to/__static;
sendfile off;
}
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
This works if the files don't exist in /path/to/__static; it sends the request to the application server. However, if the files also exist in /path/to/__static, nginx serves the files itself.
Reversing the try_files line (try_files #proxy_to_app $uri) fails in both cases. If the client requests /static/css/test.css, the application receives a request for /css/test.css, and it never seems to try /path/to/__static even though the application returns a 404.
Updated to include full configuration.
location /static/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
proxy_intercept_errors on;
error_page 404 =200 /local$uri;
}
location /local/static/ {
internal;
alias /path/to/__static/;
}