關於linux下部署JavaWeb項目,nginx負責靜態資源訪問,tomcat負責處理動態請求的nginx配置


1、項目的運行環境

  • linux版本
[root@localhost ~]# cat /proc/version
Linux version 2.6.32-358.el6.x86_64 (mockbuild@x86-022.build.eng.bos.redhat.com) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Tue Jan 29 11:47:41 EST 2013
  • jdk版本
[root@localhost ~]# java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)
  • mysql數據庫

根據linux系統下載相應的mysql版本

[root@localhost ~]# mysql --help | grep Distrib
mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper
  •  nginx靜態資源服務器
[root@localhost sbin]# nginx -v
nginx version: nginx/1.14.0
  • tomcat動態資源服務器
[root@localhost bin]# sh version.sh 
Using CATALINA_BASE:   /root/usr/tomcat8
Using CATALINA_HOME:   /root/usr/tomcat8
Using CATALINA_TMPDIR: /root/usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-3.b10.el6_9.x86_64/jre
Using CLASSPATH:       /root/usr/tomcat8/bin/bootstrap.jar:/root/usr/tomcat8/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.21
Server built:   Mar 23 2015 14:11:21 UTC
Server number:  8.0.21.0
OS Name:        Linux
OS Version:     2.6.32-358.el6.x86_64
Architecture:   amd64
JVM Version:    1.8.0_171-b10
JVM Vendor:     Oracle Corporation

 2、nginx配置

  • nginx.conf主配置
[root@localhost nginx]# cat nginx.conf 
user  root;
worker_processes 4;
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;  //加載conf.d目錄下所有以.conf結尾的站點配置文件
}
  • conf.d目錄下各站點的配置
[root@localhost nginx]# cd conf.d/
[root@localhost conf.d]# ll
總用量 8
-rw-r--r-- 1 root root 726 5月  18 11:03 host-01.conf
-rw-r--r-- 1 root root 640 5月  18 10:52 host-02.conf

host-01.conf配置: server { listen
80; server_name www.aaa.com; default_type 'text/html'; charset utf-8; access_log /root/logs-nginx/host-01-access.log main; //站點 www.aaa.com訪問日志配置 error_log /root/logs-nginx/host-01-error.log warn; //站點 www.aaa.com訪問錯誤日志配置 location ~ .*\.(gif|jpg|jpeg|png)$ { //靜態資源訪問配置 root /root/static1; //靜態資源訪問的根目錄 index index.html index.htm; } location / {//動態資源訪問配置 root html; proxy_pass http://127.0.0.1:8080; //配置的服務器地址,指向本地的tomcat服務器端口8080 proxy_read_timeout 600s; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
host-02.conf配置:
server {
    listen       80;
    server_name  www.ccc.com;

    default_type 'text/html';
    charset utf-8;

    access_log  /root/logs-nginx/host-02-access.log  main;
    error_log  /root/logs-nginx/host-02-error.log warn;

    location ~ .*\.(gif|jpg|jpeg|png|css|js)$ {
       root   /root/static; //靜態資源訪問的根目錄
       index  index.html index.htm;
    }

    location / {
        root   html;
        proxy_pass http://127.0.0.1:8080;
        proxy_read_timeout 600s;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

從上不難看出兩個站點配置同時指向同一個tomcat服務器,但是是靜態資源的訪問路徑不一樣,其中host-01.conf靜態資源根目錄root: /root/static1是不存在的,host-02.conf的靜態文件配置是對的,下面演示下效果。

由於本項目部署在內網環境中,不能直接訪問其數據庫和服務,可以通過xshell通道到本地,由於本服務的站點域名是隨意指定,無法找到,可以修改客戶端的hosts文件進行訪問,如下圖所示:

xshell通道設置:

 

修改客戶機的hosts文件(本機的host文件路徑:C:\WINDOWS\system32\drivers\etc):

在host中添加以下配置:
127.0
.0.1 www.aaa.com 127.0.0.1 www.bbb.com 127.0.0.1 www.ccc.com 127.0.0.1 www.ddd.com

下面對配置的兩個站點進行訪問

站點1:www.aaa.com

由於站點1的靜態資源配置為:location ~ .*\.(gif|jpg|jpeg|png),而配置的靜態資源目錄static1不存在,所以zt-logo.png圖片無法顯示。
下面演示下站點2:www.ccc.com,對比看看。

下面查看下nginx的日志:
站點1的訪問日志:

站點1的錯誤日志:

站在2的訪問日志:

站在2的錯誤日志:無

 對比站點1、2可以發現,域名為:www.aaa.com的訪問日志和錯誤日志分別記錄在host-01-access.log、host-01-error.log,而域名為:www.ccc.com的訪問日志和錯誤日志分別記錄在host-02-access.log、host-02-error.log中。

注意事項

  • 啟動tomcat時注意給tomcat啟動腳本添加執行權限;
  • mysql數據庫要根據系統的版本進行下載安裝
  • nginx用戶設置盡量和安裝用戶保持一致,以免權限不足無法啟動nginx或修改默認的日志文件后造成無法記錄日志的現象;
  • 在linux系統安裝mysql、jdk、nginx等可以先安裝個yum或apt-get管理工具,使用插件安裝方便快捷;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM