Docker安装Nginx

  #docker images nginx
  #docker search nginx
  #docker pull nginx

  #docker run -it -p 8084:80 --name sciibd-nginx -v $PWD/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx -v $PWD/index.html:/usr/share/nginx/index.html nginx

  #docker ps

一、安装组件

  查看gcc版本
  #gcc -v
  #yum -y install gcc     # 安装gcc编译相关使用
  #yum install -y pcre pcre-devel pcre、pcre-devel    #安装 Nginx的rewrite模块和HTTP核心模块会用到PCRE正则表达式语法
  #yum install -y zlib zlib-devel zlib             #安装 用于文件的解压缩
  #yum install -y openssl openssl-devel openssl     #安装 openssl、openssl-devel: 一般当配置https服务的时候就需要这个了

二、安装Nginx

  #wget http://nginx.org/download/nginx-1.14.2.tar.gz   # 获取nginx
  #tar -zxvf nginx-1.14.2.tar.gz -C ./           #解压nginx
  #cd ./nginx-1.14.2
  #./configure 
  #make
  #make install                    # 配置编译执行,安装完成后位置为:/usr/lib/nginx
  #cd /usr/local/nginx/sbin/ 启动目录
  #./nginx   启动     ./nginx -s stop   停止     /nginx -s reload   重启
  #./nginx -t    测试   ./nginx -v   查看版本      ./nginx -V   查看配置

三、配置Nginx

  http 反向代理配置

  nginx.conf 配置文件如下:

  注:conf / nginx.conf 是 nginx 的默认配置文件。你也可以使用 nginx -c 指定你的配置文件

#运行用户
#user nginxuser; #启动进程,通常设置成和cpu的数量相等
worker_processes ; #全局错误日志
error_log /var/log/nginx/logs/error.log;
error_log /var/log/nginx/logs/notice.log notice;
error_log /var/log/nginx/logs/info.log info; #PID文件,记录当前启动的nginx的进程ID
pid /var/log/nginx/logs/nginx.pid; #工作模式及连接数上限
events {
worker_connections ; #单个后台worker process进程的最大并发链接数
} #设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型(邮件支持类型),类型由mime.types文件定义
include /var/log/nginx/conf/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/logs/access.log main;
rewrite_log on; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on; #连接超时时间
keepalive_timeout ;
tcp_nodelay on; #gzip压缩开关
#gzip on; #设定实际的服务器列表
upstream zp_server1{
server 127.0.0.1:;
} #HTTP服务器
server {
#监听80端口,80端口是知名端口号,用于HTTP协议
listen ; #定义使用www.xx.com访问
server_name www.test.com; #首页
index index.html #指向webapp的目录
root D:\Workspace\Project\SpringNotes\spring-shiro\src\main\webapp; #编码格式
charset utf-; #代理配置参数
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr; #反向代理的路径(和upstream绑定),location 后面设置映射的路径
location / {
proxy_pass http://test_server1;
} #静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root D:\Workspace\Project\SpringNotes\spring-shiro\src\main\webapp\views;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
} #设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
} #禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
} #错误处理页面(可选择性配置)
#error_page /.html;
#error_page /50x.html;
#location = /50x.html {
# root html;
#}
}
}

多个 webapp 的配置

http {
#此处省略一些基本配置 upstream product_server{
server www.test.com:;
} upstream admin_server{
server www.test.com:;
} upstream finance_server{
server www.test.com:;
} server {
#此处省略一些基本配置
#默认指向product的server
location / {
proxy_pass http://product_server;
} location /product/{
proxy_pass http://product_server;
} location /admin/ {
proxy_pass http://admin_server;
} location /finance/ {
proxy_pass http://finance_server;
}
}
}

跨域解决方案

解决跨域问题一般有两种思路:

CORS

在后端服务器设置 HTTP 响应头,把你需要运行访问的域名加入加入 Access-Control-Allow-Origin中。

jsonp

把后端根据请求,构造 json 数据,并返回,前端用 jsonp 跨域。

在 enable-cors.conf 文件中设置 cors :

# allow origin list
set $ACAO '*'; # set single origin
if ($http_origin ~* (www.test.com)$) {
set $ACAO $http_origin;
} if ($cors = "trueget") {
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
} if ($request_method = 'OPTIONS') {
set $cors "${cors}options";
} if ($request_method = 'GET') {
set $cors "${cors}get";
} if ($request_method = 'POST') {
set $cors "${cors}post";
}

#chmod +x nginx                                        #修改配置文件权限

#service nginx start/stop/restart/status/     #启动/停止/重启/查看状态

四、卸载Nginx

#whereis nginx                                          #查找Nginx环境配置

#service nginx stop                                    #停止Nginx服务

#chkconfig nginx off                                  #查看Nginx是否停止

#rm -rf /etc/nginx/                                     #删除Nginx配置文件

#rm -rf /usr/sbin/nginx

#rm -rf /etc/init.d/nginx

#yum remove nginx           #yum移除Nginx安装

Linux、Docker安装Nginx的更多相关文章

  1. Win10 & Linux Docker 安装使用

    Docker最近推出了可以运行在Win10和Mac上的稳定版本,让我们赶紧来体验一下. 一.Windows Docker 安装 1.安装 需要的条件为: 64bit Windows 10,开启Hype ...

  2. Docker安装Nginx(含:Windows启动、重启、停止)

    Docker安装Nginx #docker pull nginx:latest (第一次启动Docker-Nginx) #docker run --detach \ --publish 80:80 \ ...

  3. 【转】linux 编译安装nginx,配置自启动脚本

    linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...

  4. linux 编译安装nginx,配置自启动脚本

    本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装nginx,记录下安装过程: 参照这篇文章:Linu ...

  5. 【Linux】Linux上安装Nginx

    本文介绍Linux环境安装Nginx,这里用的Linux系统是CentOS 7.2. 1. 从Nginx官网下载Nginx.这里用的版本为:1.13.6. 2. 将下载下来的Nginx上传到Linux ...

  6. 【云服务器部署】---Linux下安装nginx

    [云服务器部署]---Linux下安装nginx 之前两篇,分别讲了:Linux下安装MySQL  和  springboot项目部署云服务器 nginx安装也是挺简单的.具体步骤如下: 第一步,下载 ...

  7. linux 下 安装nginx及压力测试

    linux 编译安装nginx,配置自启动脚本 下载nginx: wget http://nginx.org/download/nginx-1.8.0.tar.gz下载openssl : wget h ...

  8. linux(centos7) 安装nginx

    linux(centos7) 安装nginx 1.14(stable) 版本 Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内, ...

  9. 01 linux上安装 nginx

    一:linux上安装 nginx 下载nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz 解压:tar zxvf nginx-1.6.2.t ...

随机推荐

  1. redis安装linux(二)

    官网地址:http://redis.io/ redis的安装 第一步:安装VMware,并且在VMware中安装centos系统(参考linux教程). 第二步:将redis的压缩包,上传到linux ...

  2. 网赚app

    网赚app有很多目前来说做的比较好的赚的比较多的有四款推荐 宝石星球下载地址:http://www.baoshixingqiu.com/redPacket?key=548341 雪梨网APP下载地址 ...

  3. Feign源码解析系列-最佳实践

    前几篇准备写完feign的源码,这篇直接给出Feign的最佳实践,考虑到目前网上还没有一个比较好的实践解释,对于新使用spring cloud的同学会对微服务之间的依赖产生一些迷惑,也会走一些弯路.这 ...

  4. Could not find a version that satisfies the requirement PIL

    Python Imageing Library 简称 PIL Python常用的图像处理库之一 Pillow是PIL一个fork. C:\Users\dangzhengtao>pip insta ...

  5. Vue源码之目录结构

    Vue版本:2.6.9 源码结构图 ├─ .circleci // 包含CircleCI持续集成/持续部署工具的配置文件 ├─ .github // 项目相关的说明文档,上面的说明文档就在此文件夹 ├ ...

  6. 20155208徐子涵 Exp 6 信息搜集与漏洞扫描

    20155208徐子涵 Exp 6 信息搜集与漏洞扫描 实验目的 掌握信息搜集的最基础技能与常用工具的使用方法 实验内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技 ...

  7. SDL中 so库的使用

    用到的项目:Tocy-Android-SDLv2 JAVA层:只有一个 SDLActivity.java 路径\Android-SDLv2\src\org\libsdl\app 项目简单分析: 默认在 ...

  8. ANSYS稳态热分析

    目录 题目 APDL操作 温度云图 题目 管子内径外径为r1=4.125mm,r2=4.635mm,中间物体的产热功率为Q=8.73e8W/m3,管外有温度t=127℃的冷水流过,冷却水与管子外表面的 ...

  9. Beginning Math and Physics For Game Programmers (Wendy Stahler 著)

    Chapter 1. Points and Lines (已看) Chapter 2. Geometry Snippets (已看) Chapter 3. Trigonometry Snippets  ...

  10. oracle报错 ORA-02290: 违反检查约束条件问题

    场景: 使用plsql/developer 将原本要求非空的字段   改为可以为空 然后在插入数据的时候 报错改字段约束条件还起作用 解决方案: 首先查询该表的约束条件 select * from u ...