您的功能请求是否与问题有关?
的种类。 我希望用 nginx 终止 ssl,并同时将其用作反向代理。 我在文档中没有看到这样的例子,即使我认为这是很常见的场景。
描述您想要的解决方案
只是一个关于如何使用反向代理终止 ssl 的示例配置
描述您考虑过的替代方案
我考虑过在没有这个角色的情况下应用带有 ssl 终止的 nginx 反向代理。 就像手动配置 nginx 一样,如果这太难了。
您可以使用示例并使用从模板默认值中获取的 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是完全正确的。 在某个阶段,我还想为 Molecule 剧本中涵盖的大多数用例提供工作示例,如果可能的话,包括 SSL(使用自签名证书)。
我正在尝试在下面生成此配置,但没有运气。 我想知道它是否支持。
位置 /app1/ {
proxy_pass http://localhost :6000;
}
位置 /app2/ {
proxy_pass http://localhost :5000;
}
绝对应该工作。 您使用的是最新版本还是主版本? 如果在 main 上,请检查https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml#L153 -L290 - 你会看到有两个带有proxy_pass
的位置块location
和proxy_pass
的值。 (旁注:可能想创建一个新问题,据我所知,您的问题与ssl
无关。)
我也无法配置简单的反向代理。 在上面的示例中,我不认识
反向代理:
部分。
当我尝试在我定义的服务器上使用 proxy_pass 而不是 root 时,我得到
TemplateAssertionError: no test named 'boolean'
一个工作示例将不胜感激。
reverse_proxy
部分不再存在。 它被重构为proxy
字典,您现在可以在main
(和0.4.0
)上找到它。 您是否检查了先前评论中的分子示例? 此外,服务器上下文中不允许使用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
你好,
在 python 2.7 和 ansible 2.9.6 上有同样的问题:
"TemplateAssertionError: no test named 'boolean'"
在 python 3.6 和 ansible 2.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.
在带有 Python 2.7 和 Jinja2 2.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
最有用的评论
您可以使用示例并使用从模板默认值中获取的 dict 对象扩展它们: https ://github.com/nginxinc/ansible-role-nginx/blob/master/defaults/main/template.yml
像这样的东西:
我省略了 ssl 密码、dhparam、proxy_set_header 等,因为我在主配置中设置了它们,但您可以从示例或默认值中获取它们。 使用上游也是如此。