前言
我一直對nginx不太熟,
目前也只到了會用,大部分的指令看得懂。
但湊在一起,還是要查一下。
這次的錯誤就發生在我以為的問題上
正文
又多了一個新服務要架設,
所以想當然,東西又丟到我這來了。
這次跟以前的比較不一樣,這次是一個全新的服務。
所以之前的自動佈署、yaml、nginx設定檔通通重來。
雖然說是重來,但也只是複製貼上改一改,然後就炸了XDDDD
我複製之前的設定檔像這個樣子。中間刪除了很多東西,
請不要照抄。
server_tokens off;
log_format client '$remote_addr - $remote_user [$time_local] , '
'http-host: "$http_host" , URL: "$request" , request-status : "$status" , '
'body-byte: $body_bytes_sent ,http-referer: "$http_referer" ,'
'user-agent: "$http_user_agent" , X-Forwarded-For : "$http_x_forwarded_for" , '
' request-time: "$request_time" , response_time : "$upstream_response_time" ';
client_max_body_size 100m;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain text/css application/x-javascript application/javascript application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# GCP HTTP(S) LoadBalancer will add Via, but nginx check it to disable compress by default...
gzip_proxied any;
gzip_vary on;
server {
listen 80 default_server;
listen [::]:80 default_server;
access_log /dev/stdout client;
error_log /dev/stderr;
index index.html index.htm;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location /api/socket.io/ {
proxy_pass http://rd5-api;
proxy_set_header Host $host;
proxy_set_header token $rd5_token;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header via 'proxy_pass/token-proxy ws';
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 1d;
}
location /50x.html {
root /usr/share/nginx/html/frontend-client/;
internal;
}
location /healthz {
access_log off;
return 200 'ok';
}
location / {
error_page 418 = @fjysdz;
recursive_error_pages on;
if ($host = 'yb-test.fjysdz.cn') {
return 418;
}
rewrite "^(.*)\$" $1 permanent; # 去除url尾部重複斜線
root /usr/share/nginx/html/frontend-client/$dir;
add_header via frontend-proxy;
try_files $uri $uri/ /index.html /index.htm =404;
}
}
然後就出現錯誤了
“server_tokens” directive is not allowed here
我百思不得其解,我明明是照抄的怎麼會錯。
後來將server_tokens搬進去 server 的區塊內,好了能跑了。
但又爆出
“log_format” directive is not allowed here
我又只好在最外層加上http {} ,但心中一直有一個疑問,
為什麼會失敗!?
最後變成這樣
events {
worker_connections 1024;
}
http {
log_format client '$remote_addr - $remote_user [$time_local] , '
'http-host: "$http_host" , URL: "$request" , request-status : "$status" , '
'body-byte: $body_bytes_sent ,http-referer: "$http_referer" ,'
'user-agent: "$http_user_agent" , X-Forwarded-For : "$http_x_forwarded_for" , '
' request-time: "$request_time" , response_time : "$upstream_response_time" ';
client_max_body_size 100m;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain text/css application/x-javascript application/javascript application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# GCP HTTP(S) LoadBalancer will add Via, but nginx check it to disable compress by default...
gzip_proxied any;
gzip_vary on;
server {
server_tokens off;
listen 80 default_server;
listen [::]:80 default_server;
access_log /dev/stdout client;
error_log /dev/stderr;
index index.html index.htm;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location /conf/domain {
access_log off;
default_type application/json;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET';
return 200 '{"domain":"500015", "site": "porn1" ,"cdn":"x-cdn-yb"}';
}
# frontend-client
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location /404.html {
root /usr/share/nginx/html/frontend-client/;
internal;
}
location /50x.html {
root /usr/share/nginx/html/frontend-client/;
internal;
}
location /healthz {
access_log off;
return 200 'ok';
}
location / {
rewrite "^(.*)\$" $1 permanent; # 去除url尾部重複斜線
root /usr/share/nginx/html/$dir;
add_header via frontend-proxy;
try_files $uri $uri/ /index.html /index.htm =404;
}
}
}
跑是能正常跑了,但頁面載入一直出問題,
因為nginx log正常了,就聯絡前端同仁幫看一下。
他說從伺服器取得的css context-type有問題。
查了一下,又發現一個東西沒帶。
include /etc/nginx/mime.types;
為什麼一個能跑一個不能跑???
最後發現一個重點。
結論
Nginx 的設定檔預設的位置是
/etc/nginx/nginx.conf
然後,這個檔案裡面會先寫好預設的設定。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
include /etc/nginx/conf.d/*.conf;
}
所以能跑的服務,是因為他的設定檔放在 conf.d 裡面,
所以是從 nginx.conf載入 /etc/nginx/conf.d/*.conf 的設定。
而不能跑的服務,我是將設定檔直接覆蓋 /etc/nginx/nginx.conf ,
所以上面那一些設定我都沒寫到。
ref.
0 意見:
張貼留言