首頁  >  文章  >  運維  >  怎麼使用nginx+tomcat實現靜態和動態頁面的分離

怎麼使用nginx+tomcat實現靜態和動態頁面的分離

王林
王林轉載
2023-05-12 20:34:17735瀏覽

實驗環境:windows

實驗工具:nginx、tomcat

windows下安裝nginx非常簡單,去官網下載壓縮包解壓縮後並且雙擊解壓縮目錄下的nginx.exe程式即可。然後在瀏覽器輸入localhost可出現下圖,即表示nginx已經在運作。

怎麼使用nginx+tomcat實現靜態和動態頁面的分離 

nginx的工作流程是:對外,nginx是一個伺服器,所有的請求都先請求到nginx,然後再由nginx對內網進行請求的分發到tomcat ,然後tomcat處理完請求後將資料傳送給nginx,然後由nginx傳送給用戶,整個過程對用戶的感覺就是nginx在處理用戶請求。既然這樣子,nginx肯定需要進行配置,主要的設定檔是conf資料夾下的nginx.conf,因為我主要是進行了靜態與動態分離,所以沒有進行靜態檔案緩存,也沒有進行負載平衡的配置。

#user nobody;
worker_processes 2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;


events {
  #nginx默认最大并发数是1024个用户线程
  worker_connections 1024;
}


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

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  #http1.1在请求完之后还会保留一段时间的连接,所以这里的timeout时长不能太大,也不能太小,
  #太小每次都要建立连接,太大会浪费系统资源(用户不再请求服务器)
  keepalive_timeout 65;

  #gzip on;

  server {
  #nginx监听80端口
    listen    80;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;
  #这里的/表示所有的请求
    #location / {
    #将80端口的所有请求都转发到8080端口去处理,proxy_pass代表的是代理路径
   #  proxy_pass http://localhost:8080;
     # root  html;
      # index index.html index.htm;
    #}

  #对项目名进行访问就去访问tomcat服务
  location /student_vote { 
      proxy_pass http://localhost:8080;
  }

  #对jsp和do结尾的url也去访问tomcat服务
  location ~ \.(jsp|do)$ { 
      proxy_pass http://localhost:8080;
  }
  
  #对js、css、png、gif结尾的都去访问根目录下查找
  location ~ \.(js|css|png|gif)$ { 
      root f:/javaweb;
  }


    #error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }

    # proxy the php scripts to apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #  proxy_pass  http://127.0.0.1;
    #}

    # pass the php scripts to fastcgi server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #  root      html;
    #  fastcgi_pass  127.0.0.1:9000;
    #  fastcgi_index index.php;
    #  fastcgi_param script_filename /scripts$fastcgi_script_name;
    #  include    fastcgi_params;
    #}

    # deny access to .htaccess files, if apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #  deny all;
    #}
  }


  # another virtual host using mix of ip-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}


  # https server
  #
  #server {
  #  listen    443 ssl;
  #  server_name localhost;

  #  ssl_certificate   cert.pem;
  #  ssl_certificate_key cert.key;

  #  ssl_session_cache  shared:ssl:1m;
  #  ssl_session_timeout 5m;

  #  ssl_ciphers high:!anull:!md5;
  #  ssl_prefer_server_ciphers on;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}

}

上面的設定中我把預設的location /給註解掉了,因為它會攔截所有的請求,無論是動態還是靜態,還有一個就是對靜態檔案的設定我配置成了javaweb的工作區間,接下來會說明為什麼。

因為之前寫的項目一直以來都是使用jsp內建物件來進行目錄的文件訪問,但是使用了nginx一切都需要改變,當我使用了nginx,並且項目沒有進行路徑的修改的時候,總是無法載入靜態文件,查看日誌發現這樣的錯誤:2016/05/20 18:27:30 [error] 6748#6936: *225 createfile() "f:/javaweb/student_vote/lib/images/username .png" failed (3: the system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "get /student_vote/lib/images/username.png http/1.1", host: "localhost" , referrer: "http://localhost/student_vote/index.jsp",大致資訊是根據jsp中檔案的配置,nginx將會從/stdent_vote(這是我的專案名稱)/lib/images包中找到靜態檔案,而我又不想對專案文件做太大變化,其實還有一種方法是不使用jsp的內建對象,直接使用http://localhost/username.png來代替內建對象存取靜態文件,但是這樣改要改很多的地方,所以我就直接將web-inf資料夾下的lib資料夾拷到上一個資料夾,也就是該資料夾和web-inf資料夾是兄弟資料夾的關係。

透過上述操作,就實現了動態與靜態的分離了,無圖無真相,下面展示效果圖。

怎麼使用nginx+tomcat實現靜態和動態頁面的分離

上圖可以看到server是「apache-coyote/1.1」。 tomcat的連接器就是這個。

怎麼使用nginx+tomcat實現靜態和動態頁面的分離

而上面的server可以看到是nginx,說明對外而言接收請求的伺服器是nginx。

以上是怎麼使用nginx+tomcat實現靜態和動態頁面的分離的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除