nginx安裝和前端訪問(wèn)配置以及403 Forbidden解決方案
nginx安裝
- 安裝命令(centos環(huán)境):
sudo yum install epel-release(本次安裝nginx未執(zhí)行該條命令) sudo yum install nginx
- 配置文件位置:
/etc/nginx/nginx.conf
- 啟動(dòng):
sudo systemctl start nginx
- 配置開機(jī)啟動(dòng):
sudo systemctl enable nginx
- 重啟:
/usr/sbin/nginx -s reload sudo systemctl daemon-reload
前端nginx訪問(wèn)配置
8001系統(tǒng)+10002系統(tǒng)+10003系統(tǒng)+10005系統(tǒng)
server { listen 10003; server_name web.zc.com; index index.php index.html index.htm default.php default.htm default.html; root /mnt/web/zc-web/dist; try_files $uri $uri/ /index.html; # 將所有請(qǐng)求導(dǎo) index.html #PROXY-START/ location ~ ^/prod-api(.*)$ { rewrite ^/prod-api(.*)$ $1 break; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection $connection_upgrade; proxy_http_version 1.1; # proxy_hide_header Upgrade; add_header X-Cache $upstream_cache_status; #Set Nginx Cache set $static_filedaYAOiSb 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_filedaYAOiSb 1; expires 1m; } if ( $static_filedaYAOiSb = 0 ) { add_header Cache-Control no-cache; } } #PROXY-END/ } server { listen 10005; server_name client.zc.com; index index.php index.html index.htm default.php default.htm default.html; root /mnt/web/zc-client/dist; try_files $uri $uri/ /index.html; # 將所有請(qǐng)求導(dǎo) index.html #PROXY-START/ location ~ ^/dev-api(.*)$ { rewrite ^/dev-api(.*)$ $1 break; proxy_pass http://127.0.0.1:8080; } location ~ ^/prod-api(.*)$ { rewrite ^/prod-api(.*)$ $1 break; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection $connection_upgrade; proxy_http_version 1.1; # proxy_hide_header Upgrade; add_header X-Cache $upstream_cache_status; #Set Nginx Cache set $static_filedaYAOiSb 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_filedaYAOiSb 1; expires 1m; } if ( $static_filedaYAOiSb = 0 ) { add_header Cache-Control no-cache; } } #PROXY-END/ } server { listen 10002; server_name jetlinks.zc.com; index index.php index.html index.htm default.php default.htm default.html; root /mnt/web/jetlinks/dist; try_files $uri $uri/ /index.html; # 將所有請(qǐng)求導(dǎo) index.html #PROXY-START/ location ~ ^/api(.*)$ { rewrite ^/api(.*)$ $1 break; proxy_pass http://127.0.0.1:9999; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # 【可選】開啟websocket協(xié)議 proxy_http_version 1.1; # proxy_hide_header Upgrade; add_header X-Cache $upstream_cache_status; #Set Nginx Cache set $static_filedaYAOiSb 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_filedaYAOiSb 1; expires 1m; } if ( $static_filedaYAOiSb = 0 ) { add_header Cache-Control no-cache; } } #PROXY-END/ } server { listen 8001; server_name icp.zc.com; index index.php index.html index.htm default.php default.htm default.html; root /mnt/web/icp/dist; try_files $uri $uri/ /index.html; # 將所有請(qǐng)求導(dǎo) index.html #PROXY-START/ location ~ ^/icp-api(.*)$ { rewrite ^/icp-api(.*)$ $1 break; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection $connection_upgrade; proxy_http_version 1.1; # proxy_hide_header Upgrade; add_header X-Cache $upstream_cache_status; #Set Nginx Cache set $static_filedaYAOiSb 0; if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) { set $static_filedaYAOiSb 1; expires 1m; } if ( $static_filedaYAOiSb = 0 ) { add_header Cache-Control no-cache; } } #PROXY-END/ }
nginx訪問(wèn) 403 Forbidden
[root@yutu-node3 ~]# curl http://localhost:8001 <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.20.1</center> </body> </html>
(1)檢查文件或目錄的權(quán)限,確保 Nginx 用戶(通常是 www-data
、nginx
或 nobody
)有讀取權(quán)限
ll /path/to/your/file chmod -R 755 /path/to/your/file # 確保目錄文件可讀 chown -R nginx:nginx /path/to/your/directory # 確保 Nginx 用戶有權(quán)限
(2)如果服務(wù)器啟用了 SELinux 或 AppArmor,可能會(huì)限制 Nginx 訪問(wèn)某些文件或目錄。
chcon -R -t httpd_sys_content_t /path/to/your/directory
(3)清除 Nginx 緩存并重新加載配置
nginx -s reload
【終極大法】臨時(shí)關(guān)閉 SELinux,建議避免使用:
setenforce 0
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx轉(zhuǎn)發(fā)404問(wèn)題的排查和思考
本篇文章原自當(dāng)前業(yè)務(wù)遇到的一個(gè)實(shí)際問(wèn)題,因?yàn)槭艿剿诰W(wǎng)絡(luò)環(huán)境的因素影響,所以整體排查下來(lái)耗費(fèi)了很大精力,記錄一下Nginx 轉(zhuǎn)發(fā) 404 問(wèn)題的排查和思考,需要的朋友可以參考下2023-09-09Ubuntu+Nginx+Mysql+Php+Zend+eaccelerator安裝配置文字版
把我架設(shè)lnmp網(wǎng)站的過(guò)程寫出來(lái),希望對(duì)想架設(shè)網(wǎng)站的朋友有所幫助,如有更好的辦法請(qǐng)?zhí)岢鰜?lái)2012-02-02nginx啟動(dòng)命令和默認(rèn)配置文件的使用
這篇文章主要介紹了nginx啟動(dòng)命令和默認(rèn)配置文件的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06nginx代理去掉URL前綴的實(shí)現(xiàn)方法
nginx作為一款廣泛使用的反向代理服務(wù)器,在實(shí)際應(yīng)用中,經(jīng)常需要去掉代理請(qǐng)求中的前綴,下面這篇文章主要給大家介紹了關(guān)于nginx代理去掉URL前綴的實(shí)現(xiàn)方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05nginx簡(jiǎn)單配置多個(gè)server的方法
這篇文章主要介紹了nginx簡(jiǎn)單配置多個(gè)server的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Nginx實(shí)現(xiàn)動(dòng)態(tài)封禁IP的步驟指南
在日常的生產(chǎn)環(huán)境中,網(wǎng)站可能會(huì)遭遇惡意請(qǐng)求、DDoS 攻擊或其他有害的訪問(wèn)行為,為了應(yīng)對(duì)這些情況,動(dòng)態(tài)封禁 IP 是一項(xiàng)十分重要的安全策略,本篇博客將介紹如何通過(guò) NGINX 實(shí)現(xiàn)動(dòng)態(tài)封禁 IP,從配置到自動(dòng)化的實(shí)現(xiàn)步驟,需要的朋友可以參考下2025-02-02centos8中使用yum安裝nginx的詳細(xì)過(guò)程
這篇文章主要介紹了centos8中怎樣使用yum安裝?nginx,centos8和centos7安裝nginx有點(diǎn)點(diǎn)不一樣,centos8?自帶了nginx?1.14.1?,我們先升級(jí)到新穩(wěn)定版1.20.1,具體安裝方法跟隨小編一起學(xué)習(xí)下吧2023-03-03