SIn san sun
SIn san sun

Reputation: 623

Can not configure proxy_pass in nginx

I want to pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location.

And in my /etc/nginx/nginx.conf I configure :

server { location ~ ^/api/(.*)$ {

  listen 80;
  listen [::]:80;
  location ~ ^/api/(.*)$ {
    proxy_pass 127.0.0.1:8080;
}
}
}

And when I execute this command service nginx start:

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

Output of systemctl status nginx.service

 ● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2019-11-02 20:25:51 UTC; 53s ago
     Docs: man:nginx(8)
  Process: 12219 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 9516 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
  Process: 11760 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 27870 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 11763 (code=exited, status=0/SUCCESS)

Nov 02 20:25:51 ubuntu-s-milos2611 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 02 20:25:51 ubuntu-s-milos2611 nginx[27870]: nginx: [emerg] invalid URL prefix in /etc/nginx/nginx.conf:72
Nov 02 20:25:51 ubuntu-s-milos2611 nginx[27870]: nginx: configuration file /etc/nginx/nginx.conf test failed
Nov 02 20:25:51 ubuntu-s-milos2611 systemd[1]: nginx.service: Control process exited, code=exited status=1
Nov 02 20:25:51 ubuntu-s-milos2611 systemd[1]: nginx.service: Failed with result 'exit-code'.
Nov 02 20:25:51 ubuntu-s-milos2611 systemd[1]: Failed to start A high performance web server and a reverse proxy server.

Upvotes: 0

Views: 816

Answers (2)

RoseHosting
RoseHosting

Reputation: 376

You have to use the http:// prefix in your proxy_pass directive:

proxy_pass http://127.0.0.1:8080;

Check http://nginx.org/en/docs/http/ngx_http_proxy_module.html for more details.

Upvotes: 1

Rodrigo Loza
Rodrigo Loza

Reputation: 1248

Change your config file to

server { 
  listen 80;
  listen [::]:80;
  location ~ ^/api/(.*)$ {
    proxy_pass 127.0.0.1:8080;
  }
}

You are adding location ~^/api/(.*)$ twice which is not correct.

Upvotes: 0

Related Questions