简体   繁体   English

Nginx:如何将代理从外部请求反向到特定的 localhost url

[英]Nginx: How to reverse proxy from external request to specific localhost url

In server I have a application that responding with specific url ( http://localhost:8080/bookList ).在服务器中,我有一个响应特定 url ( http://localhost:8080/bookList ) 的应用程序。 I want a specific url of this server to be proxied by nginx from external request.我希望 nginx 从外部请求代理此服务器的特定 url。

I run this app with using maven with below commands in shell (firstly build and then running)我在 shell 中使用带有以下命令的 maven 运行此应用程序(首先构建然后运行)

mvn clean install -B
mvn spring-boot:run

When I execute this command in server which apps running by shell it respond correct ;当我在服务器中执行此命令时,shell 运行的应用程序响应正确;

curl http://localhost:8080/bookList

Here is the my nginx config;这是我的 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;


           }
    }

}

But when I try with browser with url ( http://10.10.10.10/bookList ) from external I see below fault which is 502 Bad Gateway但是当我尝试使用来自外部的带有 url ( http://10.10.10.10/bookList ) 的浏览器时,我看到下面的错误是 502 Bad Gateway

在此处输入图片说明

Issue was related by selinux and solved with below commands;问题与 selinux 相关并使用以下命令解决;

setsebool -P httpd_can_network_connect 1
setenforce 0

Here is the links which explain how to modify selinux to setup nginx reverse proxy这是解释如何修改 selinux 以设置 nginx 反向代理的链接

Using NGINX and NGINX Plus with SELinux在 SELinux 中使用 NGINX 和 NGINX Plus

Setting up reverse proxies with NGINX 使用 NGINX 设置反向代理

I guess that need to make sure some of the permissions arranged as appropirate, ıf using Nginx which accept request externaly我想需要确保将某些权限安排为适当的,如果使用接受外部请求的 Nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM