rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

NginxをProxyにしてApacheのsvnにアクセス

Client -- HTTPS --> Nginx -- HTTP --> Apache --> svn

 ってなことをやりたいときの設定方法。

  • Apacheを動作させるマシンの設定
yum install subversion mod_dav_svn -y
mkdir /var/www/svn
svnadmin create /var/www/svn/repos
chown -R apache:apache /var/www/svn/repos
htpasswd -c /var/www/svn/svnusers user1
vi /etc/httpd/conf.d/subversion.conf
<Location /svn>
   DAV svn
   SVNParentPath /var/www/svn
 #   SVNListParentPath on
 #   Limit write permission to list of valid users.
 #   <LimitExcept GET PROPFIND OPTIONS REPORT>
 #   Require SSL connection for password protection.
 #   SSLRequireSSL
      AuthType Basic
      AuthName "subversion"
      AuthUserFile /var/www/svn/svnusers
      AuthzSVNAccessFile /var/www/svn/svnaccess
      require vaid-user
#      Order Allow,Deny
#      Allow from 192.168.0.0/24
#   </LimitExcept>
</Location>

もし、svnのディレクトリをブラウザから参照したかったら、下記のように書く。
参照にも認証が必要だったら、/svn/の方にも認証の設定を書く。

<Location /svn/>
   DAV svn
   SVNParentPath /var/www/svn
</Location>
<Location /svn/.+>
   DAV svn
   SVNParentPath /var/www/svn
   SVNListParentPath on
   AuthType Basic
   AuthName "subversion"
   AuthUserFile /var/www/svn/svnusers
   AuthzSVNAccessFile /var/www/svn/svnaccess
   require vaid-user
</Location>


ユーザ設定。 

vi /var/www/svn/svnaccess
[groups]
group1 = user1,new_user

[repos:/]
@group1 = rw
* =
/etc/init.d/httpd restart

user追加は下記のコマンド

htpasswd /var/www/svn/svnusers new_user
  • Nginxを動作させるマシンの設定

証明書作成

make svn.crt

Common Nameは*にする。Common Name空にしてるとcheckoutするときにエラーになる。

Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Hokkaido
Locality Name (eg, city) [Default City]:Sapporo
Organization Name (eg, company) [Default Company Ltd]:xxx
Organizational Unit Name (eg, section) []:xxx
Common Name (eg, your name or your server's hostname) []:*
Email Address []:
openssl rsa -in svn.key -out svn.key

作成した証明書を指定する。
下記の設定のmysvnは、Nginxが動作してるマシンのIPアドレスかホスト名を入れる。192.168.0.10は、Apachesvn動かしてるマシンを設定する場所。適宜読み替えてください。

server {
      listen 443 ssl;
      server_name _;

      ssl_certificate /etc/pki/tls/certs/svn.crt;
      ssl_certificate_key /etc/pki/tls/certs/svn.key;

      location / {
        proxy_redirect off;
        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_set_header X-Forwarded-Proto https;

        set $fixed_destination $http_destination;
        if ($http_destination ~ "^https://mysvn/(.+)") {
           set $fixed_destination http://192.168.0.10/$1;
        }
        proxy_set_header Destination $fixed_destination;
        proxy_pass http://redirect_host;
     }
}
/etc/init.d/nginx reload