Ansible-role-nginx-config: nginxリバースプロキシを使用したSSLターミネーションの例

作成日 2020年05月24日  ·  13コメント  ·  ソース: nginxinc/ansible-role-nginx-config

機能リクエストは問題に関連していますか?
すこし。 sslをnginxで終了し、同時にリバースプロキシとして適用したいと思います。 これはかなり一般的なシナリオだと思いますが、ドキュメントにはこの例はありません。

希望するソリューションを説明してください
リバースプロキシでSSLを終了する方法の設定例

検討した代替案を説明してください
この役割なしでsslターミネーションを使用してnginxリバースプロキシを適用することを検討しました。 これで難しすぎる場合は、nginxを手動で構成するのと同じように。

documentation

最も参考になるコメント

例を使用して、テンプレートのデフォルトから取得したdictオブジェクトを使用して展開できます: https ://github.com/nginxinc/ansible-role-nginx/blob/master/defaults/main/template.yml

このようなもの:

nginx_http_template_enable: true
nginx_http_template:
  jira_mydomain_net:
    conf_file_name: jira_mydomain_net.conf
    servers:
      first:
        listen:
          listen_public:
            ip: 0.0.0.0
            port: 443
            ssl: true
        server_name: jira.mydomain.net
        ssl:
          cert: /etc/ssl/certs/jira.mydomain.net.crt
          key: /etc/ssl/private/jira.mydomain.net.key
        access_log:
          - name: combined
            location: /var/log/nginx/jira.mydomain.net_access.log
        error_log:
          location: /var/log/nginx/jira.mydomain.net_error.log
          level: warn
        reverse_proxy:
          locations:
            default:
              location: /
              proxy_pass: http://jira01.local.mydomain.net:8080

ssl暗号、dhparam、proxy_set_headerなどは、メイン構成で設定したため省略しましたが、例またはデフォルトから取得できます。 アップストリームを使用する場合も同じです。

全てのコメント13件

例を使用して、テンプレートのデフォルトから取得したdictオブジェクトを使用して展開できます: https ://github.com/nginxinc/ansible-role-nginx/blob/master/defaults/main/template.yml

このようなもの:

nginx_http_template_enable: true
nginx_http_template:
  jira_mydomain_net:
    conf_file_name: jira_mydomain_net.conf
    servers:
      first:
        listen:
          listen_public:
            ip: 0.0.0.0
            port: 443
            ssl: true
        server_name: jira.mydomain.net
        ssl:
          cert: /etc/ssl/certs/jira.mydomain.net.crt
          key: /etc/ssl/private/jira.mydomain.net.key
        access_log:
          - name: combined
            location: /var/log/nginx/jira.mydomain.net_access.log
        error_log:
          location: /var/log/nginx/jira.mydomain.net_error.log
          level: warn
        reverse_proxy:
          locations:
            default:
              location: /
              proxy_pass: http://jira01.local.mydomain.net:8080

ssl暗号、dhparam、proxy_set_headerなどは、メイン構成で設定したため省略しましたが、例またはデフォルトから取得できます。 アップストリームを使用する場合も同じです。

@xTrekStorexは完全に正しいです。 ある段階で、可能であればSSL(自己署名証明書を使用)を含む、Moleculeプレイブックでカバーされているほとんどのユースケースの実用的な例も示したいと思います。

私は運が悪いので、以下のこの設定を生成しようとしています。 サポートされているのだろうか。

場所/ app1 / {
proxy_pass http:// localhost :6000;
}
場所/ app2 / {
proxy_pass http:// localhost :5000;
}

間違いなく動作するはずです。 最新のリリースまたはメインを使用していますか? メインの場合は、 https: //github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml#L153-L290を確認してください- proxy_passが付いた2つのロケーションブロックがあることがわかりますlocationおよびproxy_passに指定した値を設定できます。 (補足:新しい問題を作成したい場合があります。私が知る限り、あなたの質問はsslとは関係ありません。)

また、単純なリバースプロキシを構成することもできません。 上記の例では、私は認識していません

reverse_proxy:

セクション。

定義したサーバーでrootの代わりにproxy_passを使用しようとすると、次のようになります。

TemplateAssertionError: no test named 'boolean'

実用的な例をいただければ幸いです。

reverse_proxyセクションはもう存在しません。 main (および0.4.0 )で見つけることができるproxy辞書にリファクタリングされました。 前のコメントで分子の例を確認しましたか? さらに、 proxy_passはサーバーコンテキスト内では許可されていません。 常にlocation https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass内にある必要があります。

例:

nginx_config_http_template_enable: true
    nginx_config_http_template:
      - template_file     : http/default.conf.j2
        conf_file_name    : '{{ my_server_name }}.conf'
        conf_file_location: /etc/nginx/conf.d/
        servers:
          - listen:
              - port: 443
                ssl : true
                opts: []
            server_name: '{{ my_server_name }}'
            ssl:
              cert                 : '/etc/ssl/certs/{{ my_ssl_prefix }}.nginx.bundle.crt'
              key                  : '/etc/ssl/private/{{ my_ssl_prefix }}.key'
              protocols            : '{{ NGINX_PROTOCOLS }}'
              prefer_server_ciphers: true
              ciphers              : '{{ NGINX_CIPHERS   }}'
              stapling             : true
              stapling_verify      : true
            autoindex : false
            locations :
              - location: /
                root    : '{{ my_root_folder }}'
              - location: /backend
                proxy_pass: http://127.0.0.1:8080/
                proxy:
                  set_header:
                     - field: Host
                       value: $host
                     - field: X-Real-IP
                       value: $remote_addr
                     - field: X-Forwarded-For
                       value: $proxy_add_x_forwarded_for
                     - field: X-Forwarded-Proto
                       value: $scheme

これにより、

failed: [testing-snap05.MYDOMAIN.TLD] (item={'template_file': 'http/default.conf.j2', 'conf_file_name': 'testing-snap05.staging.MYDOMAIN.TLD.conf', 'conf_file_location': '/etc/nginx/conf.d/', 'servers': [{'listen': [{'port': 443, 'ssl': True, 'opts': []}], 'server_name': 'testing-snap05.staging.MYDOMAIN.TLD', 'ssl': {'cert': '/etc/ssl/certs/star.staging.MYDOMAIN.TLD.nginx.bundle.crt', 'key': '/etc/ssl/private/star.staging.MYDOMAIN.TLD.key', 'protocols': 'TLSv1.3', 'prefer_server_ciphers': True, 'ciphers': 'HIGH:!aNULL:!MD5', 'stapling': True, 'stapling_verify': True}, 'autoindex': False, 'locations': [{'location': '/', 'root': '/usr/share/nginx/html'}, {'location': '/backend', 'proxy_pass': 'http://127.0.0.1:8080/', 'proxy': {'set_header': [{'field': 'Host', 'value': '$host'}, {'field': 'X-Real-IP', 'value': '$remote_addr'}, {'field': 'X-Forwarded-For', 'value': '$proxy_add_x_forwarded_for'}, {'field': 'X-Forwarded-Proto', 'value': '$scheme'}]}}]}]}) => {"ansible_loop_var": "item", "changed": false, "item": {"conf_file_location": "/etc/nginx/conf.d/", "conf_file_name": "testing-snap05.staging.MYDOMAIN.TLD.conf", "servers": [{"autoindex": false, "listen": [{"opts": [], "port": 443, "ssl": true}], "locations": [{"location": "/", "root": "/usr/share/nginx/html"}, {"location": "/backend", "proxy": {"set_header": [{"field": "Host", "value": "$host"}, {"field": "X-Real-IP", "value": "$remote_addr"}, {"field": "X-Forwarded-For", "value": "$proxy_add_x_forwarded_for"}, {"field": "X-Forwarded-Proto", "value": "$scheme"}]}, "proxy_pass": "http://127.0.0.1:8080/"}], "server_name": "testing-snap05.staging.MYDOMAIN.TLD", "ssl": {"cert": "/etc/ssl/certs/star.staging.MYDOMAIN.TLD.nginx.bundle.crt", "ciphers": "HIGH:!aNULL:!MD5", "key": "/etc/ssl/private/star.staging.MYDOMAIN.TLD.key", "prefer_server_ciphers": true, "protocols": "TLSv1.3", "stapling": true, "stapling_verify": true}}], "template_file": "http/default.conf.j2"}, "msg": "TemplateAssertionError: no test named 'boolean'"}

右。 Jinja2の最新バージョンに更新してみてください。 テンプレートが正しく機能するには、Jinja2 2.11.xが必要です。 これは、 https://github.com/nginxinc/ansible-role-nginx-config/issues/94で見つけたエラーに関連している可能性があります。

ありがとう。 Ubuntu 20.04のパッケージとして存在しますか? それ以外の場合、推奨される更新方法は何ですか?

Jinja2はPythonパッケージです。 pip install -U Jinja2を実行してみてください。 詳細については、 https://jinja.palletsprojects.com/en/2.11.x/intro/#installationをご覧ください。

こんにちは、

Python2.7とansible2.9.6で同じ問題が発生しています:
"TemplateAssertionError: no test named 'boolean'"

Python3.6およびansible2.9.6では、すべてが正常に機能します。

変数:

nginx_config_http_template_enable: true
nginx_config_http_template:
  - template_file: http/default.conf.j2
    conf_file_name: 50_example.com.conf
    conf_file_location: /etc/nginx/conf.d/

    servers:
      - listen:
          - ip: 0.0.0.0
            port: 80

          - ip: 0.0.0.0
            port: 443
            ssl: true

        ssl:    
          cert: /etc/pki/tls/certs/example.com.crt
          key: /etc/pki/tls/private/example.com.key

        server_name: example.com
        error_page: /usr/share/nginx/html
        autoindex: false
        http_demo_conf: false

        access_log:
          - name: json
            location: /var/log/nginx/example.com-access.json.log
        error_log:
          level: warn
          location: /var/log/nginx/example.com-error.log

        locations:
          - location: /
            proxy_pass: http://127.0.0.1
            proxy:
              bind: false
              set_header:
                - field: Host
                  value: $host
                - field: X-Forwarded-For
                  value: $proxy_add_x_forwarded_for
                - field: X-Real-IP
                  value: $remote_addr
                - field: REMOTE_ADDR
                  value: $remote_addr

結果:

+#
+# Ansible managed
+#
+
+
+
+
+server {
+    listen 0.0.0.0:80;
+    listen 0.0.0.0:443 ssl;
+    server_name example.com;
+    ssl_certificate /etc/pki/tls/certs/example.com.crt;
+    ssl_certificate_key /etc/pki/tls/private/example.com.key;
+    location / {
+        proxy_bind off;
+        proxy_set_header Host $host;
+        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+        proxy_set_header X-Real-IP $remote_addr;
+        proxy_set_header REMOTE_ADDR $remote_addr;
+
+        proxy_pass http://127.0.0.1;
+
+    }
+    # redirect server error pages to the static page /50x.html
+    #
+    error_page   500 502 503 504  /50x.html;
+    location = /50x.html {
+        root   /usr/share/nginx/html;
+    }
+    access_log  /var/log/nginx/example.com-access.json.log  json;
+    error_log /var/log/nginx/example.com-error.log warn;
+}

Jinja2を更新してみてください。 最新リリース(v2.11)を使用していません。

@alessfgありがとう!

赤い出力がなかったので、Jinja2をローカルで更新するのに実際に問題があることに気づきませんでした。

bash-3.2$ pip install -U Jinja2
Collecting Jinja2
  Using cached https://files.pythonhosted.org/packages/7e/c2/1eece8c95ddbc9b1aeb64f5783a9e07a286de42191b7204d67b7496ddf35/Jinja2-2.11.3-py2.py3-none-any.whl
Requirement not upgraded as not directly required: MarkupSafe>=0.23 in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from Jinja2) (0.23)
Installing collected packages: Jinja2
  Found existing installation: Jinja2 2.10.1
    Uninstalling Jinja2-2.10.1:
      Successfully uninstalled Jinja2-2.10.1
  Rolling back uninstall of Jinja2

You are using pip version 10.0.0, however version 20.3.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Python2.7とJinja22.11.3を使用したクリーンなvirtualenvでは、すべてが正常に機能します。

(py27) bash-3.2$ pip freeze 
ansible==2.9.6
cffi==1.14.5
cryptography==3.3.2
enum34==1.1.10
ipaddress==1.0.23
Jinja2==2.11.3
MarkupSafe==1.1.1
pycparser==2.20
PyYAML==5.4.1
six==1.15.0
このページは役に立ちましたか?
0 / 5 - 0 評価