【问题标题】:Simple nginx/Angular project force HTTPS简单的 nginx/Angular 项目强制 HTTPS
【发布时间】: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 绝不是域的真正副本

【问题讨论】:

标签: angular nginx


【解决方案1】:

对于 HTTPS 强制,我通常在我的 conf 中添加类似这样的内容:

server {
    listen 80;
    server_name myapp.com;
    return 301 https://$host$request_uri;
}

然后为 HTTP 请求将被重定向到的位置创建另一个服务器块,并将所有实际配置移到那里:

server {
    listen 443;
    server_name myapp.com;

    # Move all your location blocks here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2017-08-25
    • 2014-08-15
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多