Search code examples
nginxreverse-proxy

nginx regex in location with proxy_pass


I am attempting to set up a simple reverse proxy. I want requests for http://proxy.myserver.com/X/Y/something/somethingelse to proxy to http://videoX.sY.server.local/something/somethingelse.

Using the config below I only receive 404's from nginx.

Is this possible? and if so what am I doing wrong?

nginx.conf:

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    include /etc/nginx/conf.d/*.conf;
}

proxy1.conf:

server {
  listen 80;
  server_name proxy.myserver.com;

  more_set_headers 'Access-Control-Allow-Origin: *';
  more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
  more_set_headers 'Access-Control-Allow-Credentials: true';
  more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept';

  location /(\d+)/(\d+)/ {
    if ($request_method = OPTIONS ) {
      add_header Content-Length 0;
      add_header Content-Type text/plain;
      return 204;
    }

    proxy_pass                  http://video$1.s$2.server.local;
    proxy_redirect              off;
    proxy_connect_timeout       90;
    proxy_send_timeout          90;
    proxy_read_timeout          90;
    client_max_body_size        10m;
    proxy_buffer_size           4k;
    proxy_buffers               4 32k;
    proxy_busy_buffers_size     64k;
  }

}

Thanks


Solution

  •   location /(\d+)/(\d+)/ { 
    

    should be

      location ~ /(\d+)/(\d+)/ {
    

    see also https://serverfault.com/questions/1043043/nginx-reverse-proxy-and-regexp-location