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 服务器地址
最新文章
- 修改安卓串口蓝牙app问题记录
- javascrit2.0完全参考手册(第二版) 第1章第2节:javascript的历史和使用
- C语言中数据类型取值范围的计算的理解与总结
- ruby的optparse使用小记
- 来自JavaScript Garden摘取
- 从tcp原理角度理解Broken pipe和Connection reset by peer的区别
- Java 去除utf-8类型的空格的方法
- 获取ocx运行路径的另一种方法
- Java中字符串中子串的查找共有四种方法(indexof())
- 使用rem设计移动端自适应页面一(转载)
- 1.4.2. 实现 Core Data Helper 类(Core Data 应用程序实践指南)
- Windows 黑屏问题
- 多媒体管理器解析IMultimediaManager
- vue 子组件调用父组件的方法
- [CF52C]Circular RMQ【线段树】
- java之高效操作文件
- vue路由跳转到指定页面
- 第一阶段,第二阶段,第三阶段团队github更新项目地址
- 移动端(微信等)使用 vConsole 调试 console
- joomla 的语言翻译