nginx的請求轉(zhuǎn)發(fā)配置過程
Nginx的使用(windows環(huán)境) linux也和此類似
- 去官網(wǎng)下載nginx后解壓
- 在當(dāng)前目錄打開cmd窗口輸入nginx.exe 即可啟動服務(wù)
- 若想停止當(dāng)前服務(wù) 輸入命令nginx.exe -s stop 快速關(guān)閉nginx服務(wù)。 (注意使用Ctrl+C或者直接關(guān)閉黑窗口是不能停止服務(wù)的 )
使用Nginx來完成請求的轉(zhuǎn)發(fā) 負(fù)責(zé)分配請求的轉(zhuǎn)發(fā)到的服務(wù)其他命令:
- nginx -s quit 優(yōu)雅的關(guān)閉,優(yōu)雅是指當(dāng)一個(gè)請求被處理完成之后才被關(guān)閉。
- 配置語法檢查:nginx -c ./conf/jason.conf -t 可進(jìn)行配置文件的語法檢測。
- nginx -v查看nginx的版本
- nginx -s reload 重新加載配置文件,nginx是支持熱部署的,意思就是可以在不停止服務(wù)的情況下進(jìn)行更新部署。
- nginx -s reopen 重新打開日志文件。
Nginx的概念
- 例如 一個(gè)項(xiàng)目有多個(gè)服務(wù) 1.基本業(yè)務(wù)的服務(wù) 2.文件上傳服務(wù)(阿里云OSS)
- 前端的請求都統(tǒng)一請求nginx后再由nginx來判斷請求轉(zhuǎn)發(fā)到哪個(gè)服務(wù)
配置Nginx的請求轉(zhuǎn)發(fā)過程
- 配置目錄:nginx-1.12.0/conf/nginx.conf
- 修改nginx的配置文件 把nginx的啟動服務(wù)端口改為81 避免端口沖突
- 配置nginx的請求轉(zhuǎn)發(fā)規(guī)則(注意需要在http{}括號里面添加或者修改才有用 不是https)最好復(fù)制下面的模板來修改!!!
- 修改前端的api地址統(tǒng)一為nginx的地址
Nginx.conf 配置模板
如果需要做文件上傳的功能 :
- 需要配置nginx的最大上傳文件的大小設(shè)置
- 配置nginx上傳文件大小,否則上傳時(shí)會有 413 (Request Entity Too Large) 異常
- 打開nginx主配置文件nginx.conf,找到http{},添加 client_max_body_size 1024m;
#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 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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; #} } #配置nginx的請求轉(zhuǎn)發(fā)規(guī)則 server { listen 9001;#監(jiān)聽端口 server_name localhost;#主機(jī) #請求地址url的匹配路徑(匹配規(guī)則) 只要請求地址里面包含edu_service的就會轉(zhuǎn)發(fā)到http://location:8001 location ~ /edu_service/ { proxy_pass http://localhost:8001; } #請求地址url的匹配路徑(匹配規(guī)則) 只要請求地址里面包含eduoss的就會轉(zhuǎn)發(fā)到http://location:8002 location ~ /eduoss/ { proxy_pass http://localhost:8002; } } # 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; # } #} }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx報(bào)403 forbidden錯誤 (13: Permission denied)的解決辦法
這篇文章主要介紹了Nginx報(bào)403 forbidden錯誤 (13: Permission denied)的解決辦法,引起nginx 403 forbidden通常是三種情況:一是缺少索引文件,二是權(quán)限問題,三是SELinux狀態(tài)。具體內(nèi)容詳情大家可以參考下本文2017-01-01nginx location 配置 正則表達(dá)式實(shí)例詳解
本文通過實(shí)例代碼給大家介紹了nginx location 配置 正則表達(dá)式的問題,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Nginx設(shè)置響應(yīng)超時(shí)配置的實(shí)現(xiàn)
本文詳細(xì)介紹了如何查找和修改Nginx的配置文件,包括全局配置文件、站點(diǎn)配置文件、包含文件的查找,以及客戶端超時(shí)設(shè)置、代理超時(shí)設(shè)置、FastCGI超時(shí)設(shè)置的修改方法,最后還介紹了如何在Linux系統(tǒng)中重啟Nginx服務(wù),通過這些步驟,可以有效提高Nginx的性能和穩(wěn)定性2024-10-10nginx如何通過proxy_pass設(shè)置反向代理,隱藏端口號
這篇文章主要介紹了nginx如何通過proxy_pass設(shè)置反向代理,隱藏端口號方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Nginx配置文件中l(wèi)ocation配置的多種場景
location主要做定位功能,根據(jù)uri來進(jìn)行不同的定位,下面這篇文章主要給大家介紹了關(guān)于Nginx配置文件中l(wèi)ocation配置的多種場景,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09nginx配置ssl實(shí)現(xiàn)https訪問(小白文)
安全起見,需要將之前的http接口訪問變成https訪問,所以需要配置SSL證書,本文主要介紹了nginx配置ssl實(shí)現(xiàn)https訪問,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09