RHEL8 での nginx "13: Permission denied" の解決方法

RHEL8 での nginx "13: Permission denied" の解決方法

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
2 min read
nginx

nginx を使用して EC2 RHEL8 インスタンスのポート 1080 にトラフィックをルーティングする際、 13: Permission denied というエラーに遭遇しました。この問題は SELinux に関連しています。

状況

次の行を nginx.conf に追加しました。以下の例では、説明のために nginx.conf を直接使用しています。

diff -U3 nginx.conf.old nginx.conf
--- nginx.conf.old      2021-08-27 23:04:42.527667800 +0900
+++ nginx.conf  2021-08-28 01:20:38.088408400 +0900
@@ -45,6 +45,12 @@
         include /etc/nginx/default.d/*.conf;

         location / {
+            proxy_set_header Host                   $host;
+            proxy_set_header X-Real-IP              $remote_addr;
+            proxy_set_header X-Forwarded-Host       $host;
+            proxy_set_header X-Forwarded-Server     $host;
+            proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
+            proxy_pass http://xxx.xxx.xxx.xxx:1080;
         }

         error_page 404 /404.html;

すると、次のエラーに遭遇しました。

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.

解決方法

以下のコマンドを使用して解決できました。

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

sudo: semanage: command not found が表示される場合は、以下のコマンドでインストールしてください。

sudo dnf provides /usr/sbin/semanage
sudo dnf install policycoreutils-python-utils
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at iret, Inc.
主に AWS を利用したクラウドネイティブアプリケーションの設計および開発をしています。 Japan AWS Top Engineers 2020-2023