nginx如何配置部署一個(gè)域名,多個(gè)端口
最近用基于windows下的nginx部署了服務(wù)器。
1.安裝好windows下的nginx以后
會有以下文件,找到conf下的nginx,此文件為nginx的配置文件

2.初始只有一個(gè)默認(rèn)80端口
這是nginx的默認(rèn)端口號
server {
listen 80;
server_name "你的域名";
#charset koi8-r;
#access_log logs/host.access.log main;
//你的前端打包文件路徑
location /{
root html/static/dist;
index index.html; //指定入口文件
try_files $uri $uri/ /index.html; //重定向,解決刷新頁面404問題
}
location /shopDetail/ {
try_files $uri $uri/ /shopDetail.html; //解決偽靜態(tài)頁面刷新404問題
}
//配置實(shí)現(xiàn)反向代理
location /index.php {
#rewrite ^/api(.*)$ /$1 break;
proxy_pass http://test:8080; //你的后端接口API地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
按照上面的配置以后,將前端打包文件放進(jìn)對應(yīng)路徑以后,如:html/static/dist,此時(shí)你的前端靜態(tài)網(wǎng)頁就可以通過域名來進(jìn)行訪問了,但是我們后端項(xiàng)目也要通過nginx來部署一下,才可以實(shí)現(xiàn)在線訪問。
上圖中,可以看到,后端的端口號為8080,此時(shí)我們用nginx開一個(gè)8080端口來代理前端對后端Api的訪問:
“因?yàn)槲业暮蠖隧?xiàng)目是用php-admin的CI框架來搭建的”,
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:\webroot; //指定后端項(xiàng)目文件路徑
index index.html index.htm index.php;
}
#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$ {
location /index.php{
root D:\webroot; //指定后端項(xiàng)目文件路徑
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
此時(shí)配置好反向代理以后,假如我想再部署其他幾個(gè)同域名不同端口的項(xiàng)目,又該怎么辦呢。
可以通過設(shè)置不同端口號來進(jìn)行區(qū)分:
server {
listen 8083;
server_name "同一個(gè)域名";
#charset koi8-r;
#access_log logs/host.access.log main;
location /index.php {
#rewrite ^/api(.*)$ /$1 break;
proxy_pass http://test:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /{
root html/8083/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /shoplist {
rewrite ^/shoplist(.*)$ /shoplist.htmls$1 redirect;
}
}如上圖,我可以再開一個(gè)server,監(jiān)聽8083端口,就可以通過同一個(gè)域名不同端口號部署另一個(gè)項(xiàng)目。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
使用Docker主機(jī)啟動Nginx服務(wù)器的完整步驟詳解
Docker是一個(gè)開源的容器化平臺,用于輕松地打包、部署和運(yùn)行應(yīng)用程序,而Nginx是一個(gè)高性能的開源反向代理服務(wù)器,也是一個(gè)流行的Web服務(wù)器,這篇文章主要給大家介紹了關(guān)于使用Docker主機(jī)啟動Nginx服務(wù)器的完整步驟,需要的朋友可以參考下2024-07-07
Nginx設(shè)置目錄的訪問權(quán)限實(shí)現(xiàn)訪問靜態(tài)資源
遇到Permission denied錯(cuò)誤,通常是Nginx用戶權(quán)限不足,本文就來介紹一下Nginx設(shè)置目錄的訪問權(quán)限實(shí)現(xiàn)訪問靜態(tài)資源2024-10-10
nginx+lua(openresty)實(shí)現(xiàn)黑/白名單權(quán)限控制的示例
本文介紹了如何使用Openresty進(jìn)行權(quán)限控制和灰度發(fā)布,具體通過定時(shí)器定期更新黑名單數(shù)據(jù),進(jìn)行用戶過濾和權(quán)限管控,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09

