我想将请求传递给HTTP代理服务器,proxy_pass指令在一个位置内指定。
然后在我的/etc/nginx/nginx.conf
中进行配置:
server { location ~ ^/api/(.*)$ {
listen 80;
listen [::]:80;
location ~ ^/api/(.*)$ {
proxy_pass 127.0.0.1:8080;
}
}
}
当我执行此命令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.
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.
答案 0 :(得分:1)
您必须在proxy_pass指令中使用http://前缀:
proxy_pass http://127.0.0.1:8080;
检查http://nginx.org/en/docs/http/ngx_http_proxy_module.html了解更多详细信息。
答案 1 :(得分:0)
将您的配置文件更改为
server {
listen 80;
listen [::]:80;
location ~ ^/api/(.*)$ {
proxy_pass 127.0.0.1:8080;
}
}
您两次添加location ~^/api/(.*)$
,这是不正确的。