RHEL 8 の SELinux 環境で nginx の "13: Permission Denied" を解消する

RHEL 8 の SELinux 環境で nginx の "13: Permission Denied" を解消する

SELinux のポートラベルを追加し、RHEL 8 上の nginx が非標準ポートへバインドできるようにする方法を解説します。

Takahiro Iwasa
2 min read

RHEL 8 上の nginx で 1080 のような非標準ポートをリッスンするよう設定すると、13: Permission denied が発生することがあります。多くの場合、SELinux のポートラベルが原因です。

systemd[1]: Starting The nginx HTTP and reverse proxy server...
nginx[1626]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx[1626]: nginx: [emerg] bind() to 0.0.0.0:1080 failed (13: Permission denied)
nginx[1626]: nginx: configuration file /etc/nginx/nginx.conf test failed
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: nginx.service: Failed with result 'exit-code'.
systemd[1]: Failed to start The nginx HTTP and reverse proxy server.

解決方法

解決策は、SELinux のポリシーを更新して nginx がカスタムポート 1080 を使用できるようにすることです。

Terminal window
sudo semanage port -a -t http_port_t -p tcp 1080

sudo: semanage: command not found というエラーが出た場合は、以下のコマンドで必要なパッケージをインストールしてください。

Terminal window
sudo dnf provides /usr/sbin/semanage
sudo dnf install policycoreutils-python-utils

まとめ

semanage port を使ってポート 1080http_port_t ラベルを付けると、nginx がこの非標準ポートへバインドできるようになります。

このメッセージはファイルパーミッションのエラーに見えますが、SELinux は nginx に許可されていないタイプのポートへのバインドを拒否します。

SELinux が enforcing モードのホストで nginx に非標準のリッスンポートを追加する場合は、ファイルパーミッションやファイアウォールを変更する前に、そのポートの SELinux ラベルを確認してください。

About the author

Takahiro Iwasa

Takahiro Iwasa

Software Developer

This blog shares technical notes from hands-on projects—architecture, implementation, and AWS service integrations.