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;
}
}
Related
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.
Basically I have two local application hosted on 2 different local port.
I'm trying to access http://localhost/admin which is serve via reverse_proxy but I got http://localhost/admin/main.css net::ERR_ABORTED 404 (Not Found).
Ive also tried below (without slash) but I still got the same error.
location /admin{
proxy_pass http://127.0.0.1:8090
....
}
app1.conf
server{
listen 80;
listen [::]:80;
root /var/www/first_app/dist;
index index.html;
access_log /var/log/nginx/first_app.access.log;
error_log /var/log/nginx/first_app.error.log;
location / {
try_files $uri $uri/ /index.html =404;
}
location /admin/{
proxy_pass http://127.0.0.1:8090/
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
client_max_body_size 0;
}
location ~* .(ico|jpg|png|gif|jpeg|css|swf|js|woff)$ {
access_log off;
gzip_static on;
gzip_comp_level 5;
expires 1M;
#add_header Cache-Control private;
add_header Cache-Control public;
}
}
app2.conf
server{
listen 8090;
listen [::]:8090;
root /var/www/second_app/dist;
location /{
try_files $uri /index.html;
}
}
It would be great if someone can explain to me what I'd miss? Thanks much.
I am trying to get Basic Authentification to work with Rails 3.2 nginx and Unicorn
The configuration works for hosting my site. I used the Rails Basic Authentification in the Controller but i have to many problems while testing. The .htpasswd file is also working i could restrict the access to a static site.
In the location config i tried
location /
location /home/deployer/apps/rails/current/public
location /home/deployer/apps/rails/current/
Any ideas?
This is my config:
upstream unicorn {
server unix:/tmp/unicorn.blog.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name railsserver;
root /home/deployer/apps/rails/current/public;
location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/prototyp/.htpasswd;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn;
location #unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
I tried it with this config now but it does not work
server {
listen 80 default deferred;
server_name rails.com;
root /home/deployer/apps/rails/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri /;
location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/prototyp/.htpasswd;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Do it like this to get it to work:
location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/prototyp/.htpasswd;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
You don't need the #unicorn location
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/;
}
I'm trying to set up nginx to work with my backbonejs application and api server.
The API server is external and being routed through https://website.com/api/...
Essentially, I want any non-matched urls to be routed to /index.html for the backbone app to handle.
I've tried using try_files, but that just overrides my API. I've tried setting up another location where I check if the request is a GET and also if it doesn't match register or login or api, but that also doesn't work. Here's my server so far:
server {
listen 80; ssl off;
listen 443 ssl;
server_name app.io;
ssl_certificate /etc/nginx/conf/ssl.crt;
ssl_certificate_key /etc/nginx/conf/app.key;
root /home/ubuntu/app/public;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
index index.html;
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
location ~ ^/(api)|(auth).*$ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://app.aws.af.cm;
}
location ~ ^(/(register)|(login)).*$ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# GETs only
limit_except POST {
proxy_pass https://app.aws.af.cm;
}
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
Currently, try_files overrides the API and just redirects to index.html. Any idea how I can get everything to play nicely with one another?
Here's what I want:
if / - /index.html
else if /api/*|/auth/* - external proxy
else if /login|/register - POST - external proxy
else /* - /#$1
Figured it out:
Add try_files #uri #rewrites; to Location / and also add the #rewrites function below.
server {
listen 80; ssl off;
listen 443 ssl;
server_name app.io;
ssl_certificate /opt/nginx/conf/ssl.crt;
ssl_certificate_key /opt/nginx/conf/app.key;
root /home/ubuntu/app/public;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
index index.html;
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
try_files $uri #rewrites;
}
location ~ ^/(api)|(auth)|(logout)|(register)|(login).*$ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://app.cm;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location #rewrites {
rewrite ^/.+ /#$uri redirect;
}
}