Nginx 搭建域名訪問環(huán)境的詳細過程
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;
#}
}
讓所有的請求都轉(zhuǎn)到,商品服務(wù)

2.Nginx 搭建轉(zhuǎn)發(fā)網(wǎng)關(guā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有個坑
因為nginx代理給網(wǎng)關(guān)的時候會 丟掉請求頭里的host ,所以網(wǎng)關(guān)的匹配規(guī)則沒有匹配上導(dǎo)致404

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

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

4.還有一個坑

這個匹配規(guī)則不要放前面,因為這些請求都是從上往下匹配的,匹配到了,就不往下匹配了,然而往nginx發(fā)送的域名都是 www.gulimall.com 所以,都會走第一個匹配,下面這些就匹配不上,就找不到那個接口就404了。
做好兩點:
1.請求接口 http://域名/api/xxx
2.請求頁面 http://域名
轉(zhuǎn)發(fā)給網(wǎng)關(guān),通過網(wǎng)關(guān)路由到對應(yīng)的服務(wù) !
到此這篇關(guān)于Nginx 搭建域名訪問環(huán)境的文章就介紹到這了,更多相關(guān)Nginx 域名訪問環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?403?forbidden錯誤的五種原因及詳細解決方法
這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯誤的五種原因及詳細解決方法,相信很多人對403 forbidden是什么意思有了大致的了解,那么當(dāng)我們遇到403 forbidden怎么解決呢,需要的朋友可以參考下2023-08-08
nginx服務(wù)器access日志中大量400 bad request錯誤的解決方法
這篇文章主要介紹了nginx服務(wù)器access日志中大量400 bad request錯誤的解決方法,本文結(jié)論是空主機頭導(dǎo)致的大量400錯誤日志,關(guān)閉默認(rèn)主機的日志記錄就可以解決問題,需要的朋友可以參考下2015-01-01
Nginx緩存在服務(wù)端 代理和客戶端的區(qū)別深入探索
這篇文章主要介紹了Nginx緩存在服務(wù)端 代理和客戶端的區(qū)別深入探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

