
私はしばらくNginxで動作するようにPython(Sanic)アプリケーションを入手しようとしました。アプリケーション自体はで実行されます0.0.0.0:5000
。サーバーはローカルネットワークにあり、プロジェクトホスティング/提供にのみ使用されます。私たちは、各プロジェクトをリバースプロキシするためのプライマリ(パブリック)Webサーバーを持っています。これまで、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_app
curl http://(machine’s-local-ip)/
/my_app
50X 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