docker 搭建nginx

docker pull nginx 先用docker 去把镜像拉下来

$ docker run --name tmp-nginx-container -d nginx $ docker cp tmp-nginx-container:/etc/nginx/nginx.conf /host/path/nginx.conf $ docker rm -f tmp-nginx-container

使用上三个命令,复制docker容器内的配置文件到宿主机centos上,然后删除临时容器。
上边三个命令是必须的,不然nginx.conf会被当成一个目录

接下来定义-v 容器卷(配置文件和静态资源 -p 端口映射)
$ docker run --name my-custom-nginx-container -v /host/path/nginx.conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /host/path/html:/usr/share/nginx/html:ro -d -p 80:80 nginx

接下来只要修改/host/path/nginx.conf/nginx.conf 配置文件即可生效,无需进入容器修改 进入容器方法: docker exec -it 容器ID /bin/bash

nginx.conf 基本配置:


http {
include /etc/nginx/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 /var/log/nginx/access.log main;


sendfile on;
#tcp_nopush on;


keepalive_timeout 65;


#gzip on;
server{


listen 80;
server_name www.xymapp.cn;
root /usr/share/nginx/html;
index index.html index.htm;


location = / {
proxy_pass https://www.xymapp.cn;
}



location / {
proxy_pass https://www.xymapp.cn;
}


location /apis{

if ($request_method = 'OPTIONS') { #支持ajax跨域
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}


rewrite ^.+apis/?(.*)$ /$1 break;  #过滤apis开头的请求
proxy_pass https://www.xymapp.cn;
}
location ~* .(gif|jpg|jpeg|png|css|js|ico|html)$ {
root /usr/share/nginx/html;
}


}

#include /etc/nginx/conf.d/*.conf;

}

 
nginx 路径匹配通常定义如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}

# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了

location / {
    proxy_pass http://tomcat:8080/
}

 注意:如果配置了upstream 则无法跨域,解决办法是配置两个nginx,第一个负责upstream重定向,第二个不配置upstream 直接proxy_pass 服务器地址  




原文地址:https://www.cnblogs.com/fengwenzhee/p/10234678.html