• Linux、Docker安装Nginx


    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  1;
    
    #全局错误日志
    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 1024;    #单个后台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  120;
        tcp_nodelay        on;
    
        #gzip压缩开关
        #gzip  on;
    
        #设定实际的服务器列表
        upstream zp_server1{
            server 127.0.0.1:8089;
        }
    
        #HTTP服务器
        server {
            #监听80端口,80端口是知名端口号,用于HTTP协议
            listen       80;
    
            #定义使用www.xx.com访问
            server_name  www.test.com;
    
            #首页
            index index.html
    
            #指向webapp的目录
            root D:WorkspaceProjectSpringNotesspring-shirosrcmainwebapp;
    
            #编码格式
            charset utf-8;
    
            #代理配置参数
            proxy_connect_timeout 180;
            proxy_send_timeout 180;
            proxy_read_timeout 180;
            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:WorkspaceProjectSpringNotesspring-shirosrcmainwebappviews;
                #过期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   404              /404.html;
            #error_page   500 502 503 504  /50x.html;
            #location = /50x.html {
            #    root   html;
            #}
        }
    }

    多个 webapp 的配置

    http {
    #此处省略一些基本配置
    
        upstream product_server{
            server www.test.com:8081;
        }
    
        upstream admin_server{
            server www.test.com:8082;
        }
    
        upstream finance_server{
            server www.test.com:8083;
        }
    
        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安装

  • 相关阅读:
    交换机/路由器上的 S口 F口 E口
    对称加密&非对称加密
    字节流和字符流 in Java
    Python中使用MySQL
    完全二叉树、理想二叉树满二叉树
    优化MySchool数据库设计
    关于SQL储存过程中输出多行数据
    关于本月第一天,本月最后一天的SQL代码
    SQL常见的系统存储过程
    相关子查询【SQL Server】
  • 原文地址:https://www.cnblogs.com/zengming/p/10419882.html
Copyright © 2020-2023  润新知