【问题标题】:Nginx: How to reverse proxy from external request to specific localhost urlNginx:如何从外部请求反向代理到特定的本地主机 url
【发布时间】:2020-05-12 15:49:24
【问题描述】:

在服务器中,我有一个使用特定 url (http://localhost:8080/bookList) 响应的应用程序。我希望 nginx 从外部请求中代理此服务器的特定 url。

我在 shell 中使用带有以下命令的 maven 运行这个应用程序(首先构建然后运行)

mvn clean install -B
mvn spring-boot:run

当我在服务器中执行此命令时,shell 运行的应用程序响应正确;

curl http://localhost:8080/bookList

这是我的 nginx 配置;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

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

  server {

        listen 80;
        server_name 10.10.10.10;


           location /bookList {
           proxy_pass http://localhost:8080/bookList/;
           proxy_redirect  off;
           proxy_buffer_size 128k;
           proxy_buffers 4 256k;
           proxy_busy_buffers_size 256k;


           }
    }

}

但是当我尝试使用来自外部的 url (http://10.10.10.10/bookList) 浏览器时,我看到下面的错误是 502 Bad Gateway

【问题讨论】:

  • proxy_pass http://localhost:8080/bookList/; 更改为proxy_pass http://localhost:8080;
  • 还是不行。
  • 任何错误日志条目?
  • 我通过禁用 selinux 解决了这个问题。

标签: java nginx reverse-proxy nginx-reverse-proxy nginx-config


【解决方案1】:

问题与 selinux 相关,并通过以下命令解决;

setsebool -P httpd_can_network_connect 1
setenforce 0

这里是解释如何修改 selinux 来设置 nginx 反向代理的链接

Using NGINX and NGINX Plus with SELinux

Setting up reverse proxies with NGINX

我想如果使用接受外部请求的 Nginx,则需要确保某些权限安排得当

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 2016-07-14
    • 2019-06-20
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多