nginx的一个配置,nginx gzip压缩,nginx静态缓存,nginx负载均衡

    xiaoxiao2023-09-03  151

    #user  nobody; worker_processes  8; error_log  logs/error.log; error_log  logs/error.log  notice; error_log  logs/error.log  info; #pid        logs/nginx.pid; events {     worker_connections  1024; } http {     include       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  /usr/local/nginx/logs/access.log   main;          charset utf-8;     server_names_hash_bucket_size 128;     client_header_buffer_size 64k;     large_client_header_buffers 4 64k;     client_max_body_size 200m;     ##cache######     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;     ##设置临时目录     proxy_temp_path /data/tpl_nginx_cache_dir/temp;     ##设置缓存目录为二级目录,共享内存区大小,非活动时间,最大容量,注意临时目录要跟缓存目录在同一个分区     proxy_cache_path /data/tpl_nginx_cache_dir/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;     ##cache######     sendfile        on;     #tcp_nopush     on;     #keepalive_timeout  0;     keepalive_timeout  65;     #开启Gzip压缩     gzip  on;     #不压缩临界值,大于1k的才压缩,一般不用改     gzip_min_length 1k;     #buffer相关设置     gzip_buffers 4 16;     #用了反向代理的话,末端通信是HTTP/1.0,默认是Http/1.1     #gzip_http_version 1.0;     #压缩级别,1~10,数字越大压缩的越好,时间也越长     gzip_comp_level 3;     #进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了     gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;     #跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧     gzip_vary off;     #IE6对Gzip不怎么友好,不给它Gzip了     gzip_disable "MSIE [1-6]\."; upstream fuzaianli {    server 127.0.0.1:8080 weight=1;    server 127.0.0.1:9002 weight=1; } server {    listen 80; server_name 127.0.0.1; charset utf-8; location / {    #root html; #index index.html index.htm; proxy_pass http://fuzaianli; client_max_body_size  200m; #下面的例子表示的是要跳转的真实的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; } location ^~ /project2{             proxy_pass http://fuzaianli; #下面的例子表示的是要跳转的真实的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;         } location ~ .*/project2/ {             rewrite ^/(.*)/project2/(.*)$ http://fuzaianli/project2/$2;     } location ^~ /project1 {             proxy_pass http://fuzaianli; #下面的例子表示的是要跳转的真实的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; } location ~ .*/project1/ { rewrite ^/(.*)/project1/(.*)$ http://fuzaianli/project1/$2; } location ^~ /tpl/ { proxy_pass http://fuzaianli; #下面的例子表示的是要跳转的真实的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; } location ^~ /project3/ { rewrite ^/project3/(.*)$ http://127.0.0.1:8888/project3/$1; } location ^~ /project4 { proxy_pass http://fuzaianli; #下面的例子表示的是要跳转的真实的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; } location ~ .*/project4/ { rewrite ^/(.*)/project4/(.*)$ http://fuzaianli/project4/$2; } location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {    proxy_pass http://fuzaianli;    proxy_redirect off;             proxy_set_header Host $host;    ##设置缓存共享区块,也就是keys_zone名称。             proxy_cache cache_one;    ##设置http状态码为200,302缓存时间为1小时。             proxy_cache_valid 200 302 1h;             proxy_cache_valid 301 1d;             proxy_cache_valid any 1m;    ##设置过期时间,为30天             expires 30d; } location ~ .*\.(action)(.*) {    proxy_pass http://fuzaianli;    proxy_set_header        Host $host;             proxy_set_header        X-Real-IP $remote_addr;             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(jsp)(.*) {    proxy_pass http://fuzaianli;    proxy_set_header        Host $host;             proxy_set_header        X-Real-IP $remote_addr;             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; } }

    }

    ====================================================================

    一个域名配置相关的:

    server { listen       80; #server_name  120.55.185.149;

    server_name  project.ceshi.cn;

            #正常情况下访问下面的就可以了。

    #if ( $host = 'project.ceshi.cn' ) { #    rewrite ^/(.*)$ http://project.ceshi.cn/project1/common/toIndexPage.action permanent;         # #} ##通过下面的方式实现访问:http://project.ceshi.cn location / {  proxy_pass http://10.117.21.159:7002/project1/common/toIndexPage.action;   } #if ( $host = 'project.ceshi.cn' ) { #    rewrite ^/(.*)$ http://project.ceshi.cn/project1/common/toIndexPage.action permanent; #} location ^~ /project1/ { proxy_pass http://10.117.21.159:7002; } }
    最新回复(0)