nginx安裝和前端訪問配置以及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訪問配置
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訪問 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 訪問某些文件或目錄。
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_geo模塊實(shí)現(xiàn)CDN調(diào)度的配置方法
今天小編就為大家分享一篇利用Nginx_geo模塊實(shí)現(xiàn)CDN調(diào)度的配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Nginx徹底解決Druid未授權(quán)訪問漏洞的方法
Apache Druid作為一個(gè)高性能的實(shí)時(shí)分析數(shù)據(jù)庫,憑借其出色的數(shù)據(jù)攝取和查詢能力,在海量數(shù)據(jù)分析領(lǐng)域占據(jù)了重要地位,然而,其自帶的監(jiān)控頁面帶來了一個(gè)嚴(yán)重的安全隱患:未授權(quán)訪問漏洞,本文給大家介紹了Nginx徹底解決Druid未授權(quán)訪問漏洞的方法,需要的朋友可以參考下2025-08-08
Nginx實(shí)現(xiàn)會(huì)話保持的兩種方式
在我們做Nginx負(fù)載均衡的時(shí)候經(jīng)常會(huì)遇到會(huì)話保持的問題,為了保證同一用戶session會(huì)被分配到同一臺(tái)服務(wù)器上,這時(shí)就需要會(huì)話保持,本文介紹了兩種方法,感興趣的可以了解一下2022-03-03
nginx負(fù)載均衡配置,宕機(jī)自動(dòng)切換方式
這篇文章主要介紹了nginx負(fù)載均衡配置,宕機(jī)自動(dòng)切換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
nginx配置選項(xiàng)try_files的用法及說明
這篇文章主要介紹了nginx配置選項(xiàng)try_files的用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-10-10
LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法
這篇文章主要介紹了LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法,需要的朋友可以參考下2017-07-07

