docker配置nginx代理
代理http访问解决跨域问题
前端request.js中设置要访问后端的api接口标识。前端访问后端时会自动加上server后缀。
const request = axios.create({
baseURL: '/server',
timeout: 500000000
})
nginx配置文件中配置前端访问路径和前端访问后端的api转发路径
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /server {
rewrite ^.+server/?(.*)$ /$1 break;
proxy_pass http://10.12.96.233:9090;
}
所有前端访问中带有server后缀的请求都会被重定向至proxy_pass代理指向的地址。
因此前端访问后端时相当于nginx代为访问,而后端返回结果给前端时也相当于nginx代为返回,跨域问题也就得到了解决。
代理http转https访问
https即为http+ssl,主要为了解决访问的安全性问题,因此nginx需要配置http转https访问。
配置https首先需要有ssl安全证书,我们可以自己本地生成一份。
1.创建服务器证书密钥文件 server.key:
openssl genrsa -des3 -out server.key 2048
输入密码,确认密码,自己随便定义,但是要记住,后面会用到。
2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
3.备份一份服务器密钥文件
cp server.key server.key.org
4.去除文件口令
openssl rsa -in server.key.org -out server.key
5.生成证书文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
server.csr -signkey server.key -out server.crt
``
nginx config文件中新增监听80端口配置,并将其重定向至host1的443端口
server {
listen 80;
server_name 10.12.96.233;
rewrite ^(.*)$ https://$host$1 permanent;
}
最终配置文件为:
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
client_max_body_size 20m;
server {
# listen 80;
listen 443 ssl;
server_name 10.12.96.233;
ssl_certificate /etc/nginx/ssl/eswincomputing.com.pem;
ssl_certificate_key /etc/nginx/ssl/eswincomputing.com.key;
# ssl_certificate /etc/nginx/ssl/server.crt;
# ssl_certificate_key /etc/nginx/ssl/server_nopwd.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 配置SSL协议版本
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; # 配置SSL加密算法
ssl_prefer_server_ciphers on; # 优先采取服务器算法
ssl_session_cache shared:SSL:10m; # 配置共享会话缓存大小
ssl_session_timeout 10m; # 配置会话超时时间
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /server {
rewrite ^.+server/?(.*)$ /$1 break;
proxy_pass http://10.12.96.233:9090;
}
#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 html;
}
}
server {
listen 80;
server_name 10.12.96.233;
rewrite ^(.*)$ https://$host$1 permanent;
}
}