操作說明
1、nginx目錄中html目錄下放置green 前端項(xiàng)目
監(jiān)聽端口:8181
nginx配置文件配置location時(shí)使用root方式
# root 方式
# 方式1 域名直接可訪問到 即 localhost:8181
#location / {
# root html;
# index green/index.html green/index.htm;
#}
# 方式2 域名直接可訪問到 即 localhost:8181
#location / {
# root html/green/;
# index index.html index.htm;
#}
# 方式2.1 域名直接可訪問到 即 localhost:8181
#location / {
# root html/green;
# index index.html index.htm;
#}
# 方式3 域名+/green 可訪問到 即 localhost:8181/green
#location /green/ {
# root html;
# index index.html index.htm;
#}
# 方式3.1 訪問不到green下任務(wù)資源
#location /green/ {
# root html/green/;
# index index.html index.htm;
#}
以上三種 方式結(jié)論驗(yàn)證 用root屬性指定的值是要加入到最終路徑中的,匹配條件會拼接到路徑中
即最終獲取的靜態(tài)頁面路徑為:域名 + root + 區(qū)配條件 + index
即找到 localhost:8181/html/green/index.html文章來源:http://www.zghlxwxcb.cn/news/detail-776909.html
備注:方式2 和方式2.1 用于驗(yàn)證 root 屬性的值最后的 “/“為非必須,有沒有最后一個(gè)”/” 都可以訪問到
nginx配置文件配置location時(shí)使用alias方式
# alias 方式
# 方式1 域名直接可訪問到 即 localhost:8181
#location / {
# alias html/green/;
# index index.html index.htm;
#}
# 方式1.1 訪問不到green下任務(wù)資源
#location / {
# alias html/green;
# index index.html index.htm;
#}
# 方式2 域名直接可訪問到 即 localhost:8181
#location / {
# alias html/;
# index green/index.html green/index.htm;
#}
# 方式3 域名直接可訪問到 即 localhost:8181/green
#location /green {
# alias html/green/;
# index index.html index.htm;
#}
# 方式3.1 域名直接可訪問到 即 localhost:8181/green
#location /green/ {
# alias html/green/;
# index index.html index.htm;
#}
以上三種 方式結(jié)論驗(yàn)證 用alias屬性指定的值,匹配條件不會拼接到路徑中,會直接在alias屬性的值下面去找資源
即最終獲取的靜態(tài)頁面路徑為:域名 + alias + index
即找到 localhost:8181/html/green/index.html文章來源地址http://www.zghlxwxcb.cn/news/detail-776909.html
備注:方式1 和方式1.1 用于驗(yàn)證 alias 屬性的值最后的 “/“為必須,沒有最后一個(gè)”/” 訪問不到
完整的nginx配置文件如下
#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"';
map $time_iso8601 $logdate{
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
access_log logs/access-$logdate.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8181;
server_name localhost;
access_log logs/access-$logdate.log main;
# root 方式
# 方式1 域名直接可訪問到 即 localhost:8181
location / {
root html;
index green/index.html green/index.htm;
}
# 方式2 域名直接可訪問到 即 localhost:8181
#location / {
# root html/green/;
# index index.html index.htm;
#}
# 方式2.1 域名直接可訪問到 即 localhost:8181
#location / {
# root html/green;
# index index.html index.htm;
#}
# 方式3 域名+/green 可訪問到 即 localhost:8181/green
#location /green/ {
# root html;
# index index.html index.htm;
#}
# 方式3.1 訪問不到green下任務(wù)資源
#location /green/ {
# root html/green/;
# index index.html index.htm;
#}
# 以上三種 方式結(jié)論驗(yàn)證 用root屬性指定的值是要加入到最終路徑中的,匹配條件會拼接到路徑中
# 即最終獲取的靜態(tài)頁面路徑為:域名 + root + 區(qū)配條件 + index
# 即找到 localhost:8181/html/green/index.html
# 備注:方式2 和方式2.1 用于驗(yàn)證 root 屬性的值最后的 "/"為非必須,有沒有最后一個(gè)"/" 都可以訪問到
# alias 方式
# 方式1 域名直接可訪問到 即 localhost:8181
#location / {
# alias html/green/;
# index index.html index.htm;
#}
# 方式1.1 訪問不到green下任務(wù)資源
#location / {
# alias html/green;
# index index.html index.htm;
#}
# 方式2 域名直接可訪問到 即 localhost:8181
#location / {
# alias html/;
# index green/index.html green/index.htm;
#}
# 方式3 域名直接可訪問到 即 localhost:8181/green
#location /green {
# alias html/green/;
# index index.html index.htm;
#}
# 方式3.1 域名直接可訪問到 即 localhost:8181/green
#location /green/ {
# alias html/green/;
# index index.html index.htm;
#}
# 以上三種 方式結(jié)論驗(yàn)證 用alias屬性指定的值,匹配條件不會拼接到路徑中,會直接在alias屬性的值下面去找資源
# 即最終獲取的靜態(tài)頁面路徑為:域名 + alias + index
# 即找到 localhost:8181/html/green/index.html
# 備注:方式1 和方式1.1 用于驗(yàn)證 alias 屬性的值最后的 "/"為必須,沒有最后一個(gè)"/" 訪問不到
# 后臺服務(wù);
location /fdiagnose/ {
proxy_ignore_client_abort on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:9090;
}
}
}
到了這里,關(guān)于nginx部署前端項(xiàng)目時(shí)location時(shí)root和alias配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!