我们有一个仅需使用https的网站。 如果我们删除运行https的代码,它就可以正常工作。
这是一个在docker中的nginx上运行的angular 7应用程序。它将以天蓝色托管。
Docker文件:
### STAGE 1: Build ###
# We label our stage as 'builder'
FROM node:8-alpine as builder
COPY package*.json ./
RUN npm set progress=false && npm config set depth 0 && npm cache clean --force
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app
WORKDIR /ng-app
COPY . .
## Build the angular app in production mode and store the artifacts in dist
folder
RUN $(npm bin)/ng build --prod --build-optimizer
### STAGE 2: Setup ###
FROM nginx:1.13.3-alpine
COPY /certs /etc/nginx/
## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/
COPY nginx.conf /etc/nginx/nginx.conf
## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
## From 'builder' stage copy over the artifacts in dist folder to default
nginx public folder
COPY --from=builder /ng-app/dist /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
default.conf文件:
server {
listen 80;
sendfile on;
default_type application/octet-stream;
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 256;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
最后是我们的nginx.conf文件:
# nginx Configuration File
# http://wiki.nginx.org/Configuration
# Run as a less privileged user for security reasons.
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
pid /var/run/nginx.pid;
http {
server {
listen 80;
listen 443 ssl;
server_name digitise.co.za www.digitise.co.za;
ssl_certificate /etc/nginx/ssl.crt;
ssl_certificate_key /etc/nginx/ssl.key;
location / {
proxy_pass http://127.0.0.1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
# force https-redirects
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}
}
我们在Windows环境中运行。与以前一直使用iis一样,我们对nginx还是陌生的,但是随着天蓝色的发展,我们需要使用nginx。
我们通过运行来构建图像
docker build -t digitise .
然后我们通过运行图像
docker run -p 3000:80 -p 3001:443 digitise
命中http://localhost:3001确实将我重定向到https://localhost。 击中https://localhost:3001无效。它甚至没有命中服务器。 击中http://localhost:3000会返回错误请求,普通的HTTP请求已发送到https端口