Nginx 搭建域名訪問環(huán)境的詳細(xì)過程
1.Nginx配置文件


server {
listen 80;
server_name www.gulimall.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://192.168.232.1:10001;
}
#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 /usr/share/nginx/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;
#}
}
讓所有的請(qǐng)求都轉(zhuǎn)到,商品服務(wù)

2.Nginx 搭建轉(zhuǎn)發(fā)網(wǎng)關(guān)進(jìn)行負(fù)載均衡
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream gulimall{
server 192.168.232.1:88;
}
include /etc/nginx/conf.d/*.conf;
}gulimall.conf
server {
listen 80;
server_name www.gulimall.com;
location / {
proxy_pass http://gulimall;
}
}
網(wǎng)關(guān) application.yml
- id: gulimall_host_route
uri: lb://gulimall-product
predicates:
- Host=www.gulimall.com有個(gè)坑
因?yàn)閚ginx代理給網(wǎng)關(guān)的時(shí)候會(huì) 丟掉請(qǐng)求頭里的host ,所以網(wǎng)關(guān)的匹配規(guī)則沒有匹配上導(dǎo)致404

需要配置Nginx
在gulimall.conf
location / {
proxy_set_header Host $host;
proxy_pass http://gulimall;
}

3.實(shí)現(xiàn)動(dòng)靜分離
location /static {
root /usr/share/nginx/html;
autoindex on; #開啟文件目錄結(jié)構(gòu)在網(wǎng)頁顯示
}注意路徑會(huì)拼成, /usr/share/nginx/html/static

4.還有一個(gè)坑

這個(gè)匹配規(guī)則不要放前面,因?yàn)檫@些請(qǐng)求都是從上往下匹配的,匹配到了,就不往下匹配了,然而往nginx發(fā)送的域名都是 www.gulimall.com 所以,都會(huì)走第一個(gè)匹配,下面這些就匹配不上,就找不到那個(gè)接口就404了。
做好兩點(diǎn):
1.請(qǐng)求接口 http://域名/api/xxx
2.請(qǐng)求頁面 http://域名
轉(zhuǎn)發(fā)給網(wǎng)關(guān),通過網(wǎng)關(guān)路由到對(duì)應(yīng)的服務(wù) !
到此這篇關(guān)于Nginx 搭建域名訪問環(huán)境的文章就介紹到這了,更多相關(guān)Nginx 域名訪問環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx 如何部署指定文件夾下的項(xiàng)目(本地測試)
這篇文章主要介紹了Nginx 如何部署指定文件夾下的項(xiàng)目(本地測試),分為配置vue.config.js,指定生成環(huán)境的包,配置路由模式為hash(history模式刷新后,找不到頁面),本文講解的非常詳細(xì),需要的朋友可以參考下2024-01-01
Nginx?403?forbidden錯(cuò)誤的五種原因及詳細(xì)解決方法
這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯(cuò)誤的五種原因及詳細(xì)解決方法,相信很多人對(duì)403 forbidden是什么意思有了大致的了解,那么當(dāng)我們遇到403 forbidden怎么解決呢,需要的朋友可以參考下2023-08-08
nginx服務(wù)器access日志中大量400 bad request錯(cuò)誤的解決方法
這篇文章主要介紹了nginx服務(wù)器access日志中大量400 bad request錯(cuò)誤的解決方法,本文結(jié)論是空主機(jī)頭導(dǎo)致的大量400錯(cuò)誤日志,關(guān)閉默認(rèn)主機(jī)的日志記錄就可以解決問題,需要的朋友可以參考下2015-01-01
如何利用nginx通過正則攔截指定url請(qǐng)求詳解
這篇文章主要介紹了如何利用nginx通過正則攔截指定url請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Nginx緩存在服務(wù)端 代理和客戶端的區(qū)別深入探索
這篇文章主要介紹了Nginx緩存在服務(wù)端 代理和客戶端的區(qū)別深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

