怎麼使用nginx+tomcat實現靜態和動態頁面的分離
實驗環境:windows
實驗工具:nginx、tomcat
windows下安裝nginx非常簡單,去官網下載壓縮包解壓縮後並且雙擊解壓縮目錄下的nginx.exe程式即可。然後在瀏覽器輸入localhost可出現下圖,即表示nginx已經在運作。
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資料夾是兄弟資料夾的關係。
透過上述操作,就實現了動態與靜態的分離了,無圖無真相,下面展示效果圖。
上圖可以看到server是「apache-coyote/1.1」。 tomcat的連接器就是這個。
而上面的server可以看到是nginx,說明對外而言接收請求的伺服器是nginx。
以上是怎麼使用nginx+tomcat實現靜態和動態頁面的分離的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

可以查詢 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打開 Nginx 錯誤頁,查看頁面的標題。

在雲服務器上配置 Nginx 域名的方法:創建 A 記錄,指向雲服務器的公共 IP 地址。在 Nginx 配置文件中添加虛擬主機塊,指定偵聽端口、域名和網站根目錄。重啟 Nginx 以應用更改。訪問域名測試配置。其他注意事項:安裝 SSL 證書啟用 HTTPS、確保防火牆允許 80 端口流量、等待 DNS 解析生效。

當 Nginx 服務器宕機時,可執行以下故障排除步驟:檢查 nginx 進程是否正在運行。查看錯誤日誌以獲取錯誤消息。檢查 nginx 配置語法正確性。確保 nginx 具有訪問文件所需的權限。檢查文件描述符打開限制。確認 nginx 正在偵聽正確的端口。添加防火牆規則以允許nginx流量。檢查反向代理設置,包括後端服務器可用性。如需進一步幫助,請聯繫技術支持。
