1. 程式人生 > >新兵實戰搭建CentOS7下docker+springboot專案+nginx反向代理 (三)

新兵實戰搭建CentOS7下docker+springboot專案+nginx反向代理 (三)

新兵實戰搭建CentOS7下docker+springboot專案+nginx反向代理

接下來開始部署springboot專案。 把打包好的spingboot jar包檔案通過securexftp 工具上傳到任意資料夾下,構建docker 映象。

mvn package docker:build

執行docker Image

docker run -p 5808:8080 -t dockertest/engine_three --restart=always

左邊5808是docker暴露給外部的埠 右邊8080的是專案的埠,restart=always代表自動重啟。

圖片1

使用docker ps 檢視是否docker容器已啟動。
圖片2

在瀏覽器中輸入網址驗證是否成功。
圖片3

接下來使用nginx設定反向代理

  • 檢視nginx路徑
    圖4

進入/etc/nginx路徑下
圖5

nginx.conf是總的配置檔案。

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/server/*.conf;
}

其中注意 第一列 user root; 系統預設使用者是nginx,這樣會經常導致訪問網頁permission deny的錯誤 。這裡把user指定為root使用者。

http配置中 新增server資料夾下的conf配置作為使用者自定義配置檔案。

新建server資料夾 在該資料夾下新建default.conf 檔案。配置如下。
也可以參考這個部落格

https://my.oschina.net/floristgao/blog/488161

upstream tomcat{
  server 127.0.0.1:5808 max_fails=1 fail_timeout=60s weight=10;

}



server {
    listen       80;        #監聽80埠
    server_name  192.168.150.129; #監聽的域名
    location /docker {            #轉發或處理
        proxy_pass      http://tomcat/;
#       proxy_redirect   off;
#       proxy_set_header    X-Real-IP       $remote_addr;
 #       proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    error_page   500 502 503 504  /50x.html;#錯誤頁
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

在該檔案中設定反向代理。可以實現通過訪問192.168.150.129:80/docker 對映到192.168.150.129:5808埠。儲存檔案,並重啟nginx。

systemctl restart nginx.service

如果此時重啟失敗 那就需要檢查default.conf檔案中是否ip地址配置正確。

檢查nginx服務沒問題後開啟瀏覽器測試。

systemctl status nginx.service

發現頁面報錯 error page, 開啟nginx日誌檔案看下原因吧

預設的日誌檔案路徑在 /var/log/nginx 資料夾下。

開啟error.log檔案。

圖4

發現錯誤是

connect() to 127.0.0.1:5808 failed (13: Permission denied)

很納悶 也查了很多資料 ,發現是SeLinux的導致的 。這個還不太懂是什麼 。按照這篇部落格做法可以解決。

https://blog.csdn.net/oydaybreak/article/details/46594639

setsebool -P httpd_can_network_connect 1

再次重啟nginx 發現瀏覽器可以成功訪問了 簡單的反向代理就完成了。這篇文章是將selinx是什麼的 可以有空的時候看看

https://blog.csdn.net/yanjun821126/article/details/80828908

圖5

下一步還想把前端程式碼加入到springboot中 。或者再起一個docker 放node.js環境。先佔坑。