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. Jenkins-client模式配置

    Jenkins配置master-slave模式 本来想着先写一篇jenkins安装的流程,但是现在jenkins做的已经非常完善了,有.war文件,直接在tomcat启动即可,所以这里就不多说了,小白 ...

  2. kafka工作原理介绍

    两张图读懂kafka应用: Kafka 中的术语   broker:中间的kafka cluster,存储消息,是由多个server组成的集群.  topic:kafka给消息提供的分类方式.brok ...

  3. tableview前端基础设计(初级版)

    tableView前端基础设计 实现的最终效果 操作目的:熟悉纯代码编辑TableView和常用的相关控件SearchBar.NavigationBar.TabBar等,以及布局和基本功能的实现. 一 ...

  4. JMeter在linux服务器上使用

    环境部署: 1.在Linux服务器先安装jdk:此步骤省略,可参考百度经验:https://jingyan.baidu.com/article/6b18230980c294ba59e15967.htm ...

  5. NPM,bower的安装目录

    npm安装成功的话(需设置系统环境变量,见http://www.cnblogs.com/liaocheng/p/4531898.html有介绍) npm的安装目录 安装nodejs时,如果没有设置以下 ...

  6. RESTful Levels & HATEOAS

    什么是RESTful REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的.翻译过来就是"表现层状态转化.” REST是一种软件架构风格.设计风格,而不是 ...

  7. 服务器、应用框架、MVC、MTV

    web服务器:负责处理http请求,响应静态文件,常见的有Apache,Nginx以及微软的IIS. 应用服务器:负责处理逻辑的服务器.比如php.python的代码,是不能直接通过nginx这种we ...

  8. python3 多线程爆破ftp、mysql、ssh

    当然 也支持ip 为 127.0.0.1-255 这样的 字典放到 dict 目录里 链接: https://pan.baidu.com/s/1htchOyN5hK9nmZlWfTiFzA 密码: v ...

  9. python,验证码生成

    <pre>import string import random from PIL import Image from PIL import ImageDraw from PIL impo ...

  10. 【druid 】数据库连接池

    一.数据库连接池架构 二.数据库连接池的过滤器 spi的思想,加载配置文件的Filter druid.filters.default=com.alibaba.druid.filter.stat.Sta ...