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; #} }
讓所有的請求都轉到,商品服務
2.Nginx 搭建轉發(fā)網(wǎng)關進行負載均衡
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)關 application.yml
- id: gulimall_host_route uri: lb://gulimall-product predicates: - Host=www.gulimall.com
有個坑
因為nginx代理給網(wǎng)關的時候會 丟掉請求頭里的host ,所以網(wǎng)關的匹配規(guī)則沒有匹配上導致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; #開啟文件目錄結構在網(wǎng)頁顯示 }
注意路徑會拼成, /usr/share/nginx/html/static
4.還有一個坑
這個匹配規(guī)則不要放前面,因為這些請求都是從上往下匹配的,匹配到了,就不往下匹配了,然而往nginx發(fā)送的域名都是 www.gulimall.com 所以,都會走第一個匹配,下面這些就匹配不上,就找不到那個接口就404了。
做好兩點:
1.請求接口 http://域名/api/xxx
2.請求頁面 http://域名
轉發(fā)給網(wǎng)關,通過網(wǎng)關路由到對應的服務 !
到此這篇關于Nginx 搭建域名訪問環(huán)境的文章就介紹到這了,更多相關Nginx 域名訪問環(huán)境內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx安裝nginx-rtmp-module模塊的實現(xiàn)
nginx-rtmp-module是一個用于Nginx的第三方模塊,它使Nginx能夠支持實時多媒體流的傳輸和處理,本文主要介紹了Nginx安裝nginx-rtmp-module模塊,具有一定的參考價值,感興趣的可以了解一下2025-02-02Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用
本文主要介紹了Nginx中l(wèi)imit_req模塊和limit_conn模塊的使用,通過limit_req和limit_conn模塊,可以有效實現(xiàn)精確的請求頻率和連接數(shù)控制,下面就來具體介紹一下2024-05-05關于nginx+php5.3.8+eclipse3.7工作空間的配置方法
以前用eclipse3.6時設置php服務器時完全可以在base url欄填寫自己工作空間的目錄,然后修改nginx.conf加一個alias就行了2011-11-11詳解Nginx服務器中HTTP Headers相關的模塊配置使用
這篇文章主要介紹了詳解Nginx服務器中HTTP Headers相關的模塊配置使用,包括ngx_http_headers_module與它的增強版ngx_headers_more的配置使用講解,需要的朋友可以參考下2016-01-01