centos下配置nginx反向代理tomcat等webserver

如下都以centos爲例
1. nginx安裝
1) 依賴安裝javascript

# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc


2) 下載nginx本體及緩存module
ngx_cache_purge http://labs.frickle.com/nginx_ngx_cache_purge/
nginx http://nginx.org
3) 建立用戶php

# useradd nginx


4) 編譯nginx及安裝
以上下載的2個文件放置在/home/nginx目錄下, 並解壓
進入nginx目錄(不一樣版本名稱不同), 執行編譯前配置, 注意修改最後的ngx_cache_purge插件版本css

# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --add-module=../ngx_cache_purge-x.x
# make
# make install


5) 配置nginx
參考 附件nginx.conf
6) 建立緩存目錄
以nginx配置文件中設置的目錄爲準
7) 啓動及關閉html

在安裝時已配置運行用戶爲nginx, 必須使用root用戶啓動, 進程將屬於nginx用戶
啓動java

/usr/local/nginx/sbin/nginx


關閉node

/usr/local/nginx/sbin/nginx -s stop


從新讀取配置nginx

/usr/local/nginx/sbin/nginx -s relo


8) 設置開機自啓動
在/etc/rc.local目錄中增長啓動命令便可
也能夠使用centos7之後支持的systemctl
9) 防火牆放行nginx偵聽端口
centos7如下使用iptables相關命令
centos7以上使用firewall-cmd相關命令後端

注: 若後端服務器爲tomcat注意在/etc/rc.local中先執行source /etc/profile引入JDK配置(前提是JDK的JAVA_HOME等變量設置在profile文件內)centos

 

附 nginx.conf緩存

user  nginx nginx;
# 工做進程 默認1
worker_processes  2;

error_log  /usr/local/nginx/logs/error.log crit;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    charset utf-8;
    
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    
    #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  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  60;

    tcp_nodelay on;

    client_body_buffer_size 512k;
    proxy_connect_timeout 5;
    proxy_read_timeout 60;
    proxy_send_timeout 5;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    
    # 臨時文件目錄設置
    proxy_temp_path /home/nginx/nginx_cache/proxy_temp_dir;
    # 緩存文件目錄設置 過時時間設置 這裏設置過時時間是1天(1d), 緩存最多30g
    proxy_cache_path /home/nginx/nginx_cache/proxy_cache_dir  levels=1:2  keys_zone=cache_one:200m  inactive=1d  max_size=30g;

    # 後端服務器配置
    upstream backend_server {
        # 這裏設置後端服務器權重, 最大嘗試次數, 超時
        server   127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
        server   127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;
        ip_hash;
    }
    
    server {
        # nginx偵聽端口
        listen       80;
        # 服務器名稱, 不須要和主機名一致
        server_name  myServerName;

        charset utf-8;
        
        # 關閉訪問日誌, 例如某某IP訪問了某資源
        access_log  off;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache cache_one;
            
            proxy_cache_valid  200 304 12h;
            
            proxy_cache_key $host$uri$is_args$args;
            # 這裏配置了$server_port用於應付nginx端口非80的狀況
            proxy_set_header Host  $host:$server_port;
            proxy_set_header X-Forwarded-For  $remote_addr:$server_port;
            proxy_pass http://backend_server;
            expires  1d;
        }
        
        location ~ /purge(/.*)
        {
            allow  127.0.0.1;
            deny  all;
            proxy_cache_purge  cache_one  $host$1$is_args$args;
        }
        
        location ~ .*\.(php|jsp|cgi)?$
        {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://backend_server;
        }

        #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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
相關文章
相關標籤/搜索