1. 程式人生 > >nginx 做反向代理

nginx 做反向代理

work mage all http def app jpg 請求 b-

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

nginx 做反向代理