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. 【Python】*args和**kwargs的区别

    1.*args表示将参数作为元组传给函数 通过一个函数的定义来理解’*args’的含义 修改函数的定义: >>> def fun(*args): ... print args ... ...

  2. 看到Console.WriteLine($"string")写法,一时间不理解$的用途

    参了网上资料,原来它是C# 6.0的语法糖. C# 6.0 新加上的功能:   Null-Conditional Operator 大概就是,简洁代码量,缩短一些关于为null的判断~ 旧写法: pu ...

  3. [配置] win下maven配置

    下载解压 打开环境变量 新建MAVEN_HOME 路径为maven根文件夹 PATH 末尾添加 %MAVEN_HOME%\bin; 命令行 mvn -v 检查 添加网易maven镜像库 阿里的也可以 ...

  4. C# Main函数中调用异步方法的2种实现

    As you discovered, in VS11 the compiler will disallow an async Main method. This was allowed (but ne ...

  5. ubuntu编译安装opencv

    简易安装opencv2: conda install --channel https://conda.anaconda.org/menpo opencv 或: sudo apt-get install ...

  6. QWaiteCondition思考3

    QWaitConditioin::wait() 接收一个mutex作为参数,这个mutex应该被调用线程初始化为锁定状态.在线程进入休眠状态(waite)之前,mutex会被解锁.当线程被唤醒(Wak ...

  7. Python学习之路基础篇--11-12Python基础,函数的装饰器

    对于装饰器来说,就是在不改变函数的调用的情况下,对函数的前后增加了些许功能,这完全符合函数的 开放封闭 原则.装饰器的本质 其实就是一个闭包函数. 这是一个装饰器的步骤图 ret = func(*ar ...

  8. Ubuntu19.04配置SS+pac非全局代理

    1.先安装shadowsocks-qt5 sudo apt install shadowsocks-qt5 2.因为ss是sock5代理,而shell不支持socks5代理,只支持http/https ...

  9. 在java代码中控制UI界面

    public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle saved ...

  10. vue配置 请求本地json数据

    第一步:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require('portfinder')后添加 //第一步const expres ...