M
M
Mikhailo Poberezhny2016-02-27 02:19:49
Nginx
Mikhailo Poberezhny, 2016-02-27 02:19:49

How to redirect routes in nginx?

How in nginx to redirect all routes at the beginning of which / api to the server so that they are processed there and the rest to angular?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Erokhin, 2016-02-27
@as_for_me

> and the rest on angular?
I didn’t understand at all what angular has to do with it, but that’s not the point ...
Here is part of the real config:

server {
  listen  80;
  server_name example.com;

  location /all.css {
    root /srv/vek-node/static;
  }
  location /all.css.map {
    root /srv/vek-node/static;
  }
  location /img {
    root /srv/vek-node/static;
  }
  location /vendors {
    root /srv/vek-node/static;
  }
  
  location /media {
    alias /var/www/vek_staging/media;
  }

  location /catalog/metal {
    rewrite ^ http://metal.example.com$request_uri? permanent;
  }

  location /admin {
      uwsgi_read_timeout 600;
      client_max_body_size 30m;
      include /etc/nginx/uwsgi_params;
      uwsgi_pass unix:/run/uwsgi/app/vek_staging/socket;
  }

  location /static {
    alias /var/www/vek_staging/static;
  }

  location / {
    include /etc/nginx/proxy_params;
    proxy_read_timeout 120;
    proxy_pass http://127.0.0.1:3331;
  }
}

1) The first four location directives specify the distribution of content from the static folder, while the uri does not contain /static/. Here root is overridden.
2) The fifth location directive specifies the distribution of content from the media folder, while the uri starts with /media/. The alias directive is used here.
3) The sixth location - everything that starts with /catalog/somesubpart - redirect to a subdomain with the same uri.
4) location /admin - we pass the request to the uwsgi application (Django)
5) location / - we proxy everything else to 127.0.0.1:3331 (Node.js) The
example is quite indicative, almost all popular features are used.
UPD: the first four location directives are defined rather stupidly - this is the price for recognizing them without a prefix. But in real life, this does not threaten anything - I generate this config in a gallop, specifically gulp-nunjucks. The real config is about 2 times larger.

M
Maxim, 2016-02-27
@buhantsev

For example this is what I think:

location / {
    index index.php;
    try_files $uri $uri/ /index.php?$args;

    location ~ api$ {
      try_files $uri $uri/ /api.php?$args;
    }
  }

and then (if it's php of course) something like that:
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-service.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $2;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question