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. ReentrantLock+线程池+同步+线程锁

    1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...

  2. Skynet服务热点火焰图分析

    最近花了一周时间对场景服务进行热点分析,利用以前的火焰图工具做了一点微小的贡献,分享下心得(仓库地址在https://github.com/spin6lock/skynet_systemtap_set ...

  3. timestamp时间格式

    时间戳(timestamp),通常是一个字符序列,唯一地标识某一刻的时间. 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的 ...

  4. Hibernate 相关配置

    hibernate.temp.use_jdbc_metadata_defaults 它是用来控制是否应该向JDBC元数据来确定某些设置默认值,在数据库某些服务不可用的设置为 *不*,在某些工具中开发是 ...

  5. 小妖精的完美游戏教室——人工智能,A*算法,实现篇

    //================================================================//// Copyright (C) 2017 Team Saluk ...

  6. hdu 1518 BFS

    Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? I ...

  7. 聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用

    1.用python实现K均值算法 import numpy as np x = np.random.randint(1,100,20)#产生的20个一到一百的随机整数 y = np.zeros(20) ...

  8. Linux下安装Python3的django并配置mysql作为django默认数据库(转载)

    我的操作系统为centos6.5 1  首先选择django要使用什么数据库.django1.10默认数据库为sqlite3,本人想使用mysql数据库,但为了测试方便顺便要安装一下sqlite开发包 ...

  9. JAVA for(i = 0; i<a.length; i++) 解析

    下列 System.out.printf 语句输出的结果是什么? Char a[]={„a‟,‟b‟,‟c‟,‟d‟,‟e‟}; For(i=0; i<=a.length/2; i++) { c ...

  10. Codeforces Round #350 (Div. 2) C. Cinema

    Moscow is hosting a major international conference, which is attended by n scientists from different ...