目錄
一:下載解壓
http://nginx.org/en/download.html
二:配置
找到nginx.conf文件後打開:
我的修改後的全部:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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;
keepalive_timeout 65;
#gzip on;
server {
listen 8087;
server_name localhost;
charset uft-8;
#charset koi8-r;
#access_log logs/host.access.log main;
location /images {
alias D:\Users\MACHENIKE\pic\housepic;
#root html;
#index index.html index.htm;
expires 1d;
}
#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;
# }
#}
}
前輩們的有註釋的配置版本 :
#user nobody;
#指定nginx進程數
worker_processes 1;
#全局錯誤日誌及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 連接數上限
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型,類型由mime.type文件定義
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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
sendfile on;
#tcp_nopush on;
#連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
#開啓gzip壓縮 ,壓縮html
#gzip on;
#設定負載均衡的服務器列表 支持多組的負載均衡,可以配置多個upstream 來服務於不同的Server.
#nginx 的 upstream 支持 幾 種方式的分配
#1)、輪詢(默認) 每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
#2)、weight 指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。 跟上面樣,指定了權重。
#3)、ip_hash 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
#4)、fair
#5)、url_hash #Urlhash
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的機率越大
#1.down 表示單前的server暫時不參與負載
#2.weight 默認爲1.weight越大,負載的權重就越大。
#3.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
#server 192.168.1.116 down;
#server 192.168.1.116 backup;
server 192.168.1.121 weight=1;
server 192.168.1.122 weight=2;
}
#配置代理服務器的地址,即Nginx安裝的服務器地址、監聽端口、默認地址
server {
#1.偵聽80端口
listen 80;
#對於server_name,如果需要將多個域名的請求進行反向代理,可以配置多個server_name來滿足要
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默認主頁目錄在nginx安裝目錄的html子目錄。
root html;
index index.html index.htm;
proxy_pass http://mysvr; #跟載均衡服務器的upstream對應
}
#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;
# }
#}
}
其他:設置編碼: charset uft-8; 設置緩存過期時間爲1天:expires 1d;
記得寫分號。
測試緩存配置有沒有成功:
1、建議完成之後的步驟(步驟三、四、其他)再測試緩存。不然可能找不到訪問靜態文件的url。訪問文件夾下的靜態文件:
2、瀏覽器F12打開檢查,選擇network選項卡:↓ 。勾上關閉緩存,每次刷新,狀態都返回200。
3、不勾上關閉緩存,刷新頁面,然後發現返回的狀態爲304:↓ 。也就是成功使用了緩存。
4、查看緩存的時間:↓ 。爲86400秒,正好一天,說明緩存配置正確。
三、啓動測試:
cmd 進入Nginx解壓目錄 執行以下命令
start nginx : 啓動nginx服務
查看是否啓動 (啓動後有兩個PID會話 ): tasklist /fi "imagename eq nginx.exe"
其他命令:
nginx -s reload :修改配置後重新加載生效
nginx -s reopen :重新打開日誌文件
nginx -t -c /path/to/nginx.conf 測試nginx配置文件是否正確
驗證配置是否正確: nginx -t 查看Nginx的版本號:nginx -V 啓動Nginx:start nginx 快速停止或關閉Nginx:nginx -s stop 正常停止或關閉Nginx:nginx -s quit 配置文件修改重裝載命令:nginx -s reload |
start nginx 。
四、項目中測試
nginx裏的配置:
springboot裏的配置:
注意幾個關鍵的東西的對應。↑
代碼裏,
存文件時:真實文件保存在file.path裏,然後數據庫儲存 除了file.path,後面的文件夾和文件名。就是存後半部分
String path = StringUtils.substringAfterLast(localFile.getAbsolutePath(), filePath);
然後需要取出文件的時候,就這麼使用 ↓ 。就是把文件的實際路徑放進去。
最後取文件的時候要將nginx打開!
完成。
其他:
單獨測試打開文件:
https://blog.csdn.net/kingscoming/article/details/79042874
https://blog.csdn.net/aaa333qwe/article/details/79461639