Linux、Docker安裝Nginx

Docker安裝Nginxjavascript

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

  #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 html

  #docker ps前端

1、安裝組件java

  查看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服務的時候就須要這個了node

2、安裝Nginxnginx

  #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   查看配置web

3、配置Nginx正則表達式

  http 反向代理配置spring

  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:\Workspace\Project\SpringNotes\spring-shiro\src\main\webapp;

        #編碼格式
        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:\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   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/     #啓動/中止/重啓/查看狀態

4、卸載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安裝

相關文章
相關標籤/搜索