1、Nginx的常用配置大家可以去搜一下,有很多優秀的博客,我這篇文章要實現的需求是:
a.根據訪問的域名不同,跳轉到不同的項目(html首頁,80端口)
b.攔截訪問中所有帶有api的請求,轉發到後端的不同服務器中(Tomcat項目,任意端口)
2、下面是Nginx的nginx.conf配置文件
user root root;
worker_processes 1;
#worker_cpu_affinity 1;
worker_rlimit_nofile 60000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 60000;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
#ws
# map $http_upgrade $connection_upgrade {
# default upgrade;
# '' close;
# }
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "msie6";
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save
some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
access_log off;
include vhost/*.conf;
}
所有自定義的配置 都在vhost文件夾下面
3、現在貼出兩個vhost文件夾下面的自定義配置
server {
listen 80;
server_name dev.hr.static.baidu.com;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico|js|css|html|woff2|woff)$ {
root /deploy/www/baidu-www/web-hr;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
}
server {
listen 80;
server_name dev.am.static.baidu.com;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico|js|css|html|woff2|woff)$ {
root /deploy/www/baidu-www/web-am;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
}
這個是監聽80端口,注意server_name,這個服務名跟你訪問的host要一致纔可以實現,如果你要進入到hr的首頁,那麼你在瀏覽器的輸入是dev.hr.static.baidu.com,這樣才能正常訪問到你指定的頁面
server {
listen 80;
server_name am.dev.baidu.com;
access_log logs/am.baidu.com_nginx.log combined;
index index.html index.jsp index.php;
location ^~ /api {
proxy_pass http://127.0.0.1:8081/;#需要代理的地址
include proxy.conf;
}
location ^~ / {
root /deploy/www/baidu-www/web-am/;
}
}
這個是需要轉發的後端服務,這個路徑後面可以帶項目名,同樣也需要注意server——name
下面是反向proxy.conf的配置
表示使nginx阻止HTTP應答代碼爲400或者更高的應答
#proxy_intercept_errors on;
proxy_redirect off;
#以下三行,目的是將代理服務器收到的用戶的信息傳到真實服務器上
#後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header Accept-Encoding '';
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
#允許客戶端請求的最大單文件字節數
client_max_body_size 10m;
#緩衝區代理緩衝用戶端請求的最大字節數
client_body_buffer_size 128k;
#nginx跟後端服務器連接超時時間(代理連接超時)
proxy_connect_timeout 300;
#連接成功後,後端服務器響應時間(代理接收超時)
proxy_read_timeout 900;
#設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffer_size 32k;
#proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設置
proxy_buffers 4 64k;
#高負荷下緩衝大小(proxy_buffers*2)
proxy_busy_buffers_size 128k;
#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
proxy_temp_file_write_size 64k;
proxy_send_timeout 900;
proxy_hide_header Vary;
最後附上Nginx的重啓命令
進入到Nginx的安裝目錄下的sbin目錄,執行./nginx -t 檢查Nginx的配置文件是否正確;執行./nginx -s reload 重啓服務