lxyuma BLOG

開発関係のメモ

centos7でunix domain socketが読めない

最近、goji(golang web framework)触り始めていて、

centos7のnginxからunix domain socket経由で、

gojiに繋ごうとしても繋がらなかった。

原因はすごい下らない単純な理由だったが、メモ。

※ちなみに、よくあるsocketファイルのuser/権限の話ではない。systemdの話

事象

nginxでdomain socketを指定してbrowserアクセスすると、以下エラー

  • web上
502 Bag Gateway
connect() to 
unix:/tmp/go.sock failed 
(2: No such file or directory) 
while connecting to upstream, 
client: **.**.**.**, server: _, 
request: "GET / HTTP/1.1", 
upstream: "http://unix:/tmp/go.sock:/", 
host: "localhost:8080"

原因

/tmp配下にdomain socket置くのがよくないらしい

詳細

domain socketの記事でよく/tmp/***.sock経由で通信するものがあるが、

最近のfedora系はsecurityの都合上、/tmp経由のsocket通信を禁止してるらしい。

つい少し前のcentosでは/tmpでもdomain socket動いていた記憶があるのだが

いつの間にやら最近のcentosも、同じく/tmpのdomain socketはダメな様子。

nginx の起動ファイル

もう少し調べていくと、具体的にはcentos7のsystemd管理の話らしく、

nginxの以下の起動ファイルのPrivateTmp=trueによる挙動らしい。

# cat /etc/systemd/system/multi-user.target.wants/nginx.service
[Unit]
・・・略・・・
[Service]
・・・略・・・
PrivateTmp=true

[Install]
・・・略・・・

参考記事

対応

参考記事に習って、

/tmpでなく、/runに

domain socket入れたら動いた

補足

ちなみに、/runに手動で追加したdirの内容は再起動するとなくなってしまうので、

以下の手順で、systemdからの登録が必要。

  • 一時ファイル用の設定を追加
$ vim /etc/tmpfiles.d/my_app.conf

# 以下、追加
d /var/run/my_app 0755 user_name user_name -
$ systemd-tmpfiles --create /etc/tmpfiles.d/my_app.conf

$ systemctl daemon-reload

参考記事

ソース

goji

gojiはdocumentどこにあるかわからず(まだ全然ない?)

srcから辿って以下でdomain socket指定した。

goji.ServeListener(bind.Socket("/run/my_app/go.sock"))

nginx

いつものやつ。

upstream backend {
    server unix:/run/my_app/go.sock;
}

server {
    listen  80;
    server_name _;
    root        /usr/share/nginx/html;

        location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass http://backend/;
        }
}