Handle similar location with different blocks - nginx

I have a react app that I would like to show for a specific path. A similar path is used for the backend, though. This is a legacy app and other static files are shown using a different endpoint.
I'm not able to show the content appropriately. I'm either showing the old files, or I'm corrupting the similar path, or NGINX is appending infinity /index.html at the end of the path.
If it sounds confusing what I wrote, maybe the illustration below will help:
location / -> leave uwsgi to handle the requests
location /static/ -> serve static folder
location /request/ -> serve new react app
location /request_auth/ -> serve new react app
location /request_auth/token... -> leave uwsgi to handle the requests
This is what I have at the moment and it is not working!
server {
listen 80;
server_name myapp.com;
root /var/www/myapp/static/;
location / {
uwsgi_pass unix:///var/www/myapp/uwsgi/uwsgi.sock;
include uwsgi_params;
uwsgi_param HTTPS on;
}
# I think I can remove this block...
location ^~ /static/ {
alias /var/www/myapp/static/;
}
location ^~ /request/ {
alias /var/www/myapp/reactstatic/;
}
# from down here it is not working properly. I've tried using
# location =/request_auth/
# location /request_auth/
# and other variations in the other block as well. All unsuccessful
location ^~ /request_auth/ {
alias /var/www/myapp/reactstatic/;
}
location ~^/request_auth/[a-zA-Z0-9]+ {
uwsgi_pass unix:///var/www/myapp/uwsgi/uwsgi.sock;
include uwsgi_params;
uwsgi_param HTTPS on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Turns out I was getting weird redirects. Also, my react app had a static folder and the second uwsgi was trying to handle that.
This is what I did and it's working, but I'm sure there is a better solution.
server {
listen 80;
server_name myapp.com;
root /var/www/myapp/static/;
location / {
uwsgi_pass unix:///var/www/myapp/uwsgi/uwsgi.sock;
include uwsgi_params;
uwsgi_param HTTPS on;
}
# I think I can remove this block...
location ^~ /static/ {
alias /var/www/myapp/static/;
}
location ^~ /request/ {
alias /var/www/myapp/reactstatic/;
}
location ^~ /request/ {
alias /var/www/myapp/reactstatic/static;
}
location ^~ /request_auth/ {
alias /var/www/myapp/reactstatic/;
}
location ^~ /request_auth/ {
alias /var/www/myapp/reactstatic/static;
}
location ~^/request_auth/[a-zA-Z0-9]+ {
uwsgi_pass unix:///var/www/myapp/uwsgi/uwsgi.sock;
include uwsgi_params;
uwsgi_param HTTPS on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Related

Remove query string from nginx

I'm having some trouble with an nginx.conf. I have a front end application which appends this code to the URL after a user logs in. I'm trying to have nginx remove this ?code=randomcode. I tried writing an nginx rewrite rule, but it is conflicting with another rule I have in place it seems. Below is the nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri$args $uri$args/ /index.html;
location /app1 {
try_files $uri$args $uri$args/ /index.html;
rewrite ^/app1(/.*) $1;
break;
}
location /app1/ {
set $args '';
rewrite ^/app1/(.*)$ /app1/$1 permanent;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
I am new to writing directives for nginx config files, so the logic may not make sense, but basically with the last rewrite rule written furthest down is causing all the JS files to report 404 (Not Found).
Any advice would be appreciated.

Nginx reverse proxy to multiple sites on different locations

Is it possible to configure nginx reverse proxy where http://localhost/a/ leads to 1 site and http://localhost/b/ to another? I tried this config:
server {
listen 80;
server_name localhost;
location /a/ {
proxy_pass http://apache.org/;
}
location /b/ {
proxy_pass http://www.gnu.org/;
}
it almost works, but all the links returned by web pages are missing /a/ or /b/ prefix so it can't load any pictures, styles and etc. For example link http://localhost/css/styles.css is not working, but http://localhost/a/css/styles.css is working.
Is there a directive that will append all links on page with suitable prefix? Or there is different approach to put a websites on separate locations?
#ivan-shatsky, Thank you very much.
Just wanted to add working config if some1 else needs.
map $http_referer $prefix {
~https?://[^/]+/a/ a;
default b;
}
server {
listen 80;
server_name localhost;
location / {
try_files /dev/null #$prefix;
}
location /a/ {
proxy_pass http://apache.org/;
}
location #a {
proxy_pass http://apache.org;
}
location /b/ {
proxy_pass http://www.gnu.org/;
}
location #b {
proxy_pass http://www.gnu.org;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}```

How to Proxy Pass from / to /index.html

I'm currently working on a JS Project, that uses the url path. Now if I go on my website with example.com/, the JavaScript won't work, because I actually need example.com/index.html.
I'm already using an reverse proxy to proxy pass to two different docker containers. So my idea was to pass the request to example.com/index.html when example.com/ is called. But I can't figure out the regex stuff to achieve this goal.
My old config:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Stuff I tried:
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location / {
proxy_pass http://structure.nocms:80/index.html;
}
location ~* \S+ {
proxy_pass http://structure.nocms:80;
}
location /cdn {
proxy_pass http://content.nocms:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
The accepted answer has one disadvantage: going to example.com explicitly redirects to example.com/index.html (that is, returns 301 Moved permanently), which is not always desired.
Instead, I suggest to prepend location / with another directive, location = /, which is designed to the root URL only:
location = / {
proxy_pass http://structure.nocms:80/index.html;
}
location / {
proxy_pass http://structure.nocms:80;
}
The above instructs nginx to pass requests to example.com directly to http://structure.nocms:80/index.html, while requesting any other URLs in example.com/* would pass the request to the corresponding URL in the downstream.
Below config should work for you
server {
listen 80;
server_name example.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1G;
# optimize downloading files larger than 1G - refer to nginx doc
before adjusting
#proxy_max_temp_file_size 2G;
location = / {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://structure.example:80;
}
location /cdn {
proxy_pass http://content.example:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Serving 2 servers on one domain/port pair

I have some static HTML/Javascript/CSS files that I'd like to serve at /.
But I also have a webserver that performs all of my API calls written in Python using Flask and uwsgi.
What I'm trying to do is to have all of my static content be accessible as localhost and my web API be accessible through localhost/api.
This is my default site in sites-enabled:
server {
listen 80;
server_name localhost;
root /var/www;
location /api {
location / {
try_files $uri #app;
}
location #app {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
}
}
As you can see I'm serving static content located at /var/www and I'm trying to make all requests to /api to be handled by uwsgi..
Currently when I try this, uwsgi gives me 404 and I think that it is because the uwsgi parameters aren't being passed.
From what I can gather of the documentation (http://flask.pocoo.org/docs/deploying/uwsgi/), the method you choose only works when the app is set to the URL root. I removed the try_files from your /api location as I do not believe it is needed since you are not serving static files from there. You may not need the rewrite either.
server {
listen 80;
server_name localhost;
root /var/www;
location / {
try_files $uri $uri/ =404
}
location = /api { rewrite ^ /api/; }
location /api {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /api;
uwsgi_modifier1 30;
uwsgi_pass 127.0.0.1:3031;
}

Nginx error pages not working

I have the following vhost entry
server {
listen 80;
server_name example.com www.example.com;
#access_log /var/log/nginx/nginx-access.log;
location /media/ {
root /home/luke/django/solentcms;
}
location /admin/media/ {
root /home/luke/virts/django1.25/lib/python2.7/site-packages/django/contrib/admin/media;
}
location / {
proxy_pass http://127.0.0.1:8001;
}
error_page 404 /404.html;
location = /404.html {
root /home/luke/django/solentcms/404;
allow all;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/luke/django/solentcms/404;
allow all;
}
}
However, 404's and 50x errors are still be re-directed to the horrible nginx default pages.Any ideas as to why? This syntax works on one of my other servers.
Cheers.
Are the errors coming from your backend? You may need to add proxy_intercept_errors on; alongside your proxy_pass.

Resources