My nginx vhost config content:
server {
listen 80;
server_name t.xianzhi.xxx.domain;
access_log /data/log/nginx/t.xianzhi.xxx.domain_access.log main;
location ~ /\. {deny all;}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location = / {
root /data/web/static/html;
index index.html;
}
location / {
proxy_pass http://127.0.0.1:9000;
}
location = /favicon.ico {
access_log off;
root /data/web/static/;
}
location = /apple-app-site-association {
add_header Content-Type "text/html; charset=utf-8";
root /data/web/show/public/wap/;
}
location ~ \.(css|js|png|jpg|woff|ttf)$ {
root /data/web/static;
expires 10d;
}
}
As the config, I want to server the path / to /data/web/static/html/index.html and server the others to proxy_pass.
The truth is the path / is 404 not found and the others is successful.
The log is :
24/Aug/2017:10:49:43 +0800 10.5.17.37 t.xianzhi.xxx.domain - curl/7.51.0 - request:GET / HTTP/1.1 bbs:233status:404 upad:127.0.0.1:9000 rspt:0.017 rqtt:0.017 request_body:-
So, the / is passed to proxy.
Some info:
The nginx version: nginx/1.10.1
So, how to fix it?
The problem is your = / location block. If you consider the section
location = / {
root /data/web/static/html;
index index.html;
}
You specify the root and index.html, but you don't server anything. So you should change it to
location = / {
root /data/web/static/html;
index index.html;
try_files /index.html =404;
}
or
location = / {
root /data/web/static/html;
try_files /index.html =404;
}
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 have two location directives both containing conditional redirects.
server {
listen 443 ssl;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/cert.key;
server_name services.gixxx.de;
location / {
if (-f $document_root/maintenance.on) {
return 503;
}
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
}
location /api {
if (-f $document_root/maintenance.on) {
return 503;
}
proxy_read_timeout 120;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_pass http://serviceapp;
}
upstream serviceapp {
server serviceapp:3000;
}
When I create a document name maintenance.on on the route folder it works for the first location / { directive but not for location /api { part.
What is going wrong here.
You have no root defined for the second block. You should move the root statement into the outer block and allow it to be inherited by both location blocks.
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;
}
I need to:
point site.com to my site.github.io (that works)
point site.com/anything to filesystem /site/dist (that works)
point site2.com to site.com to filesystem /root/dist (that doesn't work, here it shows site.github.io)
site.com.conf
server {
listen 80;
server_name site.com;
location = / {
proxy_pass http://site.github.io;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
root /site/dist;
try_files $uri /index.html;
}
}
site2.com.conf
server {
listen 80;
server_name site2.com;
proxy_set_header Host site.com;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://127.0.0.1/$request_uri;
}
}
In the case http://site2.com you're going to end up with uri = "/" when hitting site.com, which you have set to proxy to site.github.io.
You may want to do instead for site2 is:
location / {
proxy_pass http://127.0.0.1/site2/$request_uri;
}
Then in site.com:
location ~ ^/site2(.*)$ {
root /site/dist;
try_files $1 /index.html;
}
OR
location /site2 {
rewrite ^/site2(.*)$ $1 break;
root /site/dist;
try_files $uri /index.html;
}
I have an application which is developed using angularjs and the entire application loads when the dist/ folder is accessed.
What am trying todo is, that when page is not found on angularjs, to try on a reverse proxy, I tried to do the below setup but nginx does not allow setting up the same location twice in single block
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
client_max_body_size 10M;
root /var/lib/www/dist;
charset utf-8;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/lib/www/dist;
try_files $uri $uri/ /index.html =404;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Connection "";
if (!-f $request_filename) {
proxy_pass http://app_root;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/lib/app/etc/templates;
}
}
so basically, if the URL 404'ed on angularjs I want it to try and pass it to proxy_pass http://app_root; any one can advise on how to achieve this setup?
Thanks,
UPDATE
So am trying approach proposed by "Mohammad AbuShady", and updated my nginx settings to following, but still not working, instead it tries to find the page in the AngularJS app and not move to the #proxy up_stream setup
upstream app_root {
server unix:/tmp/app_root.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
client_max_body_size 10M;
root /var/lib/www/dist;
charset utf-8;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/lib/www/dist;
try_files $uri$args $uri$args/ $uri/ /index.html #proxy;
}
location #proxy {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
if (!-f $request_filename) {
proxy_pass http://app_root;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/lib/app/etc/templates;
}
}
You're over thinking it, a single location can handle it and then give it a fallback
location / {
# omitted extra settings
# check notes below
try_files $uri #proxy;
}
location #proxy {
# omitted proxy settings
proxy_pass http://app_root;
}
Notes:
No need for a second root inside location, it's already defined in
server block
I've removed $uri/ because you don't have index in your server.
I also removed /index.html, if you do want to use it then you might want to define it as index in the server block and put the $uri/ back
server {
index index.html;
location / {
try_files $uri $uri/ #proxy;
}
}
I have no idea where app_root is, but I'm assuming it's an upstream defined somewhere else.
Try this Mo:
server {
listen 80;
server_name example.com;
keepalive_timeout 60;
client_max_body_size 10M;
root /var/lib/www/dist;
charset utf-8;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
root /var/lib/www/dist;
try_files $uri $uri/ /index.html =404;
error_page 404 = #404;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Connection "";
if (!-f $request_filename) {
proxy_pass http://app_root;
break;
}
}
location #404 {
proxy_pass http://app_root;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/lib/app/etc/templates;
}
}