【发布时间】:2017-11-17 20:47:31
【问题描述】:
我有一个非常简单的 Docker 容器,在 nginx v1.13 中运行一个 Angular 应用程序,我想在该容器上强制执行 HTTPS。
我已经尝试设置重写规则,但我要么只让它在根域上工作(即:http://myapp.com -> https://myapp.com),而不是在页面上(即:http://myapp.com/login 保持 http)
我尝试了 $localhost、$host、$server_name 和 $request_uri 的不同组合,但我通常会遇到“重定向过多”错误。
我的域有一个 SSL 证书,它可以与 IIS 中托管的其他应用程序一起使用,所以我很确定这只是我的 nginx 配置。
这是配置文件(与图片相同):
```
服务器{ 听 80; server_name 本地主机;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /(assets)/ {
gzip_static on;
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
expires off;
}
#error_page 404 /404.html;
# 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
```
更新:
感谢帮助,但原来是 Azure 配置问题。我必须设置自定义域选项以允许 SSL 重定向。
在本地运行容器时,@MatTheWhale 的建议似乎对我的服务产生了影响,尽管我无法 100% 确认它,因为localhost 绝不是域的真正副本
【问题讨论】:
-
Read the SSL termination documentation 用于 Nginx。