nginx 实践配置
阅读原文时间:2023年07月11日阅读:1

nginx.conf文件

user root;
worker_processes 1;

error_log logs/error.log crit;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
include ccproxy.conf;
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  logs/access.log  main;

    server\_names\_hash\_bucket\_size     256;  
    client\_body\_buffer\_size      128k;  
    client\_header\_buffer\_size    8k;  
    client\_max\_body\_size            50m;  
    client\_header\_timeout        1m;  
    client\_body\_timeout                1m;  
    large\_client\_header\_buffers 4 8k;

    send\_timeout        3m;

sendfile        on;  
tcp\_nopush     on;

#keepalive\_timeout  0;  
keepalive\_timeout  120;  
    tcp\_nodelay                    on;

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;

    upstream cctest1.com {  
        server 127.0.0.1:8080 weight=5;  
        server 127.0.0.1:9080 weight=5;  
    }

server {  
    listen       80;  
    server\_name  ccserver1;

    #charset koi8-r;

    access\_log  logs/host.access.log  main;

            index index.html index.htm index.jsp;  
            root /usr/common/apache-tomcat-7.0.78\_1/webapps/ROOT/;

            location ~\* ^/arch1/.\*\\.(jpg|jpeg|gif|png|swf|ico)$ {  
                root /usr/common/apache-tomcat-7.0.78\_1/webapps;  
            }

            location ~\* ^/arch1/.\*\\.(html|htm|js|css)$ {  
                root /usr/common/apache-tomcat-7.0.78\_1/webapps;  
            }

            location ~\* .\*\\.(jpg|jpeg|gif|png|swf|ico)$ {  
                if (-f $request\_filename) {  
                    #expires   15d;  
                    break;  
                }  
            }

            location ~\* .\*\\.(html|htm|js|css)$ {  
                if (-f $request\_filename) {  
                    #expires   1d;  
                }  
            }

    location / {  
            proxy\_pass    http://cctest1.com;

    }

    #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 {  
        internal;  
        root errors/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;  
#    }  
#}

}

mime.types文件

types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;

text/mathml                           mml;  
text/plain                            txt;  
text/vnd.sun.j2me.app-descriptor      jad;  
text/vnd.wap.wml                      wml;  
text/x-component                      htc;

image/png                             png;  
image/tiff                            tif tiff;  
image/vnd.wap.wbmp                    wbmp;  
image/x-icon                          ico;  
image/x-jng                           jng;  
image/x-ms-bmp                        bmp;  
image/svg+xml                         svg svgz;  
image/webp                            webp;

application/font-woff                 woff;  
application/java-archive              jar war ear;  
application/json                      json;  
application/mac-binhex40              hqx;  
application/msword                    doc;  
application/pdf                       pdf;  
application/postscript                ps eps ai;  
application/rtf                       rtf;  
application/vnd.ms-excel              xls;  
application/vnd.ms-powerpoint         ppt;  
application/vnd.wap.wmlc              wmlc;  
application/vnd.google-earth.kml+xml  kml;  
application/vnd.google-earth.kmz      kmz;  
application/x-7z-compressed           7z;  
application/x-cocoa                   cco;  
application/x-java-archive-diff       jardiff;  
application/x-java-jnlp-file          jnlp;  
application/x-makeself                run;  
application/x-perl                    pl pm;  
application/x-pilot                   prc pdb;  
application/x-rar-compressed          rar;  
application/x-redhat-package-manager  rpm;  
application/x-sea                     sea;  
application/x-shockwave-flash         swf;  
application/x-stuffit                 sit;  
application/x-tcl                     tcl tk;  
application/x-x509-ca-cert            der pem crt;  
application/x-xpinstall               xpi;  
application/xhtml+xml                 xhtml;  
application/zip                       zip;

application/octet-stream              bin exe dll;  
application/octet-stream              deb;  
application/octet-stream              dmg;  
application/octet-stream              eot;  
application/octet-stream              iso img;  
application/octet-stream              msi msp msm;

application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;  
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;  
application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

audio/midi                            mid midi kar;  
audio/mpeg                            mp3;  
audio/ogg                             ogg;  
audio/x-m4a                           m4a;  
audio/x-realaudio                     ra;

video/3gpp                            3gpp 3gp;  
video/mp4                             mp4;  
video/mpeg                            mpeg mpg;  
video/quicktime                       mov;  
video/webm                            webm;  
video/x-flv                           flv;  
video/x-m4v                           m4v;  
video/x-mng                           mng;  
video/x-ms-asf                        asx asf;  
video/x-ms-wmv                        wmv;  
video/x-msvideo                       avi;  

}

ccproxy.conf文件

#!proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
proxy_set_header X-Forward-For $Proxy_add_x_forwarded_for;#获取代理者的真实IP
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 64k;

动态代理 请求header