Browse Source

nginx config for matrix

Bob Mottram 8 years ago
parent
commit
883d483797
1 changed files with 101 additions and 1 deletions
  1. 101
    1
      src/freedombone-app-matrix

+ 101
- 1
src/freedombone-app-matrix View File

@@ -47,6 +47,103 @@ matrix_variables=(ONION_ONLY
47 47
                   MATRIX_SECRET
48 48
                   DEFAULT_DOMAIN_NAME)
49 49
 
50
+function matrix_nginx {
51
+    matrix_proxy_str='
52
+    location /matrix {
53
+        proxy_pass https://localhost:8448;
54
+        proxy_buffering on;
55
+    }'
56
+
57
+    if [ ! -f /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME} ]; then
58
+        matrix_nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
59
+        if [[ $ONION_ONLY == "no" ]]; then
60
+            function_check nginx_http_redirect
61
+            nginx_http_redirect $DEFAULT_DOMAIN_NAME
62
+            echo 'server {' >> $matrix_nginx_site
63
+            echo '  listen 443 ssl;' >> $matrix_nginx_site
64
+            echo '  listen [::]:443 ssl;' >> $matrix_nginx_site
65
+            echo "  server_name $DEFAULT_DOMAIN_NAME;" >> $matrix_nginx_site
66
+            echo '' >> $matrix_nginx_site
67
+            echo '  # Security' >> $matrix_nginx_site
68
+            function_check nginx_ssl
69
+            nginx_ssl $DEFAULT_DOMAIN_NAME
70
+
71
+            function_check nginx_disable_sniffing
72
+            nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
73
+
74
+            echo '  add_header Strict-Transport-Security max-age=15768000;' >> $matrix_nginx_site
75
+            echo '' >> $matrix_nginx_site
76
+            echo '  # Logs' >> $matrix_nginx_site
77
+            echo '  access_log /dev/null;' >> $matrix_nginx_site
78
+            echo '  error_log /dev/null;' >> $matrix_nginx_site
79
+            echo '' >> $matrix_nginx_site
80
+            echo '  # Root' >> $matrix_nginx_site
81
+            echo "  root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $matrix_nginx_site
82
+            echo '' >> $matrix_nginx_site
83
+            echo '  # Index' >> $matrix_nginx_site
84
+            echo '  index index.html;' >> $matrix_nginx_site
85
+            echo '' >> $matrix_nginx_site
86
+            echo '  # Location' >> $matrix_nginx_site
87
+            echo '  location / {' >> $matrix_nginx_site
88
+            function_check nginx_limits
89
+            nginx_limits $DEFAULT_DOMAIN_NAME '15m'
90
+            echo '  }' >> $matrix_nginx_site
91
+            echo '' >> $matrix_nginx_site
92
+            echo '  # Restrict access that is unnecessary anyway' >> $matrix_nginx_site
93
+            echo '  location ~ /\.(ht|git) {' >> $matrix_nginx_site
94
+            echo '    deny all;' >> $matrix_nginx_site
95
+            echo '  }' >> $matrix_nginx_site
96
+            echo '}' >> $matrix_nginx_site
97
+        else
98
+            echo -n '' > $matrix_nginx_site
99
+        fi
100
+        echo 'server {' >> $matrix_nginx_site
101
+        echo "    listen 127.0.0.1:$MATRIX_PORT default_server;" >> $matrix_nginx_site
102
+        echo "    server_name $DEFAULT_DOMAIN_NAME;" >> $matrix_nginx_site
103
+        echo '' >> $matrix_nginx_site
104
+        function_check nginx_disable_sniffing
105
+        nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
106
+        echo '' >> $matrix_nginx_site
107
+        echo '  # Logs' >> $matrix_nginx_site
108
+        echo '  access_log /dev/null;' >> $matrix_nginx_site
109
+        echo '  error_log /dev/null;' >> $matrix_nginx_site
110
+        echo '' >> $matrix_nginx_site
111
+        echo '  # Root' >> $matrix_nginx_site
112
+        echo "  root /var/www/$DEFAULT_DOMAIN_NAME/htdocs;" >> $matrix_nginx_site
113
+        echo '' >> $matrix_nginx_site
114
+        echo '  # Location' >> $matrix_nginx_site
115
+        echo '  location / {' >> $matrix_nginx_site
116
+        function_check nginx_limits
117
+        nginx_limits $DEFAULT_DOMAIN_NAME '15m'
118
+        echo '  }' >> $matrix_nginx_site
119
+        echo '' >> $matrix_nginx_site
120
+        echo '  # Fancy URLs' >> $matrix_nginx_site
121
+        echo '  location @matrix {' >> $matrix_nginx_site
122
+        echo '    rewrite ^(.*)$ /index.php?p=$1 last;' >> $matrix_nginx_site
123
+        echo '  }' >> $matrix_nginx_site
124
+        echo '' >> $matrix_nginx_site
125
+        echo '  # Restrict access that is unnecessary anyway' >> $matrix_nginx_site
126
+        echo '  location ~ /\.(ht|git) {' >> $matrix_nginx_site
127
+        echo '    deny all;' >> $matrix_nginx_site
128
+        echo '  }' >> $matrix_nginx_site
129
+        echo '}' >> $matrix_nginx_site
130
+
131
+        if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
132
+            function_check create_site_certificate
133
+            create_site_certificate $DEFAULT_DOMAIN_NAME 'yes'
134
+        fi
135
+
136
+        nginx_ensite $DEFAULT_DOMAIN_NAME
137
+    fi
138
+
139
+    if ! grep 'localhost:8448' /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}; then
140
+        sed "s|:443 ssl;|:443 ssl;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
141
+        sed "s| default_server;| default_server;${matrix_proxy_str}|g" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
142
+    fi
143
+
144
+    systemctl restart nginx
145
+}
146
+
50 147
 function matrix_generate_synapse_file {
51 148
     local filepath="${1}"
52 149
 
@@ -130,10 +227,11 @@ function add_user_matrix {
130 227
     ${PROJECT_NAME}-pass -u $new_username -a matrix -p "$new_user_password"
131 228
 
132 229
     read_config_param 'MATRIX_SECRET'
230
+    matrix_nginx
133 231
     if [ -f /var/lib/matrix/.synapse/bin/activate ]; then
134 232
         source /var/lib/matrix/.synapse/bin/activate
135 233
     fi
136
-    register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml https://localhost:${MATRIX_PORT} -u "${new_username}" -p "${new_user_password}" -a
234
+    register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml https://${DEFAULT_DOMAIN_NAME}/matrix -u "${new_username}" -p "${new_user_password}" -a
137 235
     if [ ! "$?" = "0" ]; then
138 236
         echo '1'
139 237
     else
@@ -382,6 +480,8 @@ function install_matrix {
382 480
     rm -f ${MATRIX_DATA_DIR}/.procmailrc
383 481
     rm -f ${MATRIX_DATA_DIR}/.emacs-mutt
384 482
 
483
+    matrix_nginx
484
+
385 485
     if [[ $(add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}") != "0" ]]; then
386 486
         echo $'Failed to add matrix admin user';
387 487
         exit 879352