Nginx의 ProxyPass가 예상대로 작동하지 않습니다.

Nginx의 ProxyPass가 예상대로 작동하지 않습니다.

나는 한동안 Nginx와 함께 작동하도록 Python(Sanic) 애플리케이션을 얻으려고 노력해 왔습니다. 애플리케이션 자체는 에서 실행됩니다 0.0.0.0:5000. 서버는 로컬 네트워크에 있으며 프로젝트 호스팅/제공에만 사용됩니다. 우리는 각 프로젝트를 역방향 프록시하는 기본(공용) 웹 서버를 보유하고 있습니다. 이 정도까지 Python 애플리케이션을 실행하는 서버는 프로젝트를 직접 제공하지 않기 때문에 도메인 이름이 없습니다(우리 프록시 서버에는 도메인 이름이 있고 공개적으로 액세스할 수 있습니다). 프록시 서버에서는 로컬 IP에서 호스트 서버만 참조합니다. 이 접근 방식은 다른 여러 서버(Apache 기반)에서 작동했지만 최근에 이 서버를 구입했고 Nginx를 사용해 보고 싶었습니다.

상황에 따라 Apache를 사용해 보았으나 제대로 작동하지 않았으므로 제가 놓친 근본적인 문제가 있을 수 있다고 생각됩니다. 나는 단지 내 nginx.conf아이디어가 타당한지 확인하고 싶었고(나는 Nginx를 처음 접했기 때문에) 누군가 나에게 문제를 일으킬 수 있는 문제에 대한 제안이 있는지 확인하고 싶었습니다.

마지막 맥락입니다. exec를 통해 또는 프로젝트 호스트에서 로컬로 서비스에 액세스할 수 있습니다 curl http://0.0.0.0:5000/my_app. 포트를 지정하지 않으면 작동하지 않습니다. 프록시 서버에서는 기본 Nginx html 응답을 반환하는 실행을 통해서만 프로젝트 호스트에 액세스할 수 있습니다. 엔드포인트와 통신을 시도하면 .curl http://(machine’s-local-ip):5000/my_appcurl http://(machine’s-local-ip)//my_app50X error

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 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

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

        location / {
        }

        location /my_app/ {
            proxy_pass http://0.0.0.0:5000/my_app/;
            proxy_redirect off;
            proxy_buffering off;

            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header Host "machine’s-local-ip";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

답변1

그래서 추가 조사와 nginx 로그를 살펴본 후 이것이 SELinux 문제라는 것을 알게 되었습니다. 다음과 같이하세요http://alfredoroca.github.io/nginx/selinux/2017/03/13/Allowing-Nginx-to-use-a-Puma-Unicorn-UNIX-socket-with-SELinux이 문제를 해결했습니다. 완전성을 위해 아래에 게시하겠습니다.

$ sudo grep nginx /var/log/audit/audit.log | audit2allow -m nginx > nginx.te
$ cat nginx.te
# cat output
# require {
#     type unconfined_t;
#     type httpd_t;
#     type httpd_sys_content_t;
#     class sock_file write;
#     class unix_stream_socket connectto;
#     class capability2 block_suspend;
# }
#
# ============= httpd_t ==============
# allow httpd_t httpd_sys_content_t:sock_file write;
# allow httpd_t self:capability2 block_suspend;
# allow httpd_t unconfined_t:unix_stream_socket connectto; 
$ checkmodule -M -m -o nginx.mod nginx.te
$ semodule_package -o nginx.pp -m nginx.mod
$ sudo semodule -i nginx.pp
$ sudo systemctl restart nginx

관련 정보