1. 程式人生 > >nginx多站點配置

nginx多站點配置

cnblogs director real 數據 doc cor file all 設置

主配置文件 nginx.conf:

user www;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  102400;
}


http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See 
http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; }

站點1配置文件:one.conf

server {
    listen       80;
    server_name  www.xx.cn;

    #charset koi8-r;

    access_log  /data/logs/wwwxx.access.log  main;

    location / {
        root   /data/wwwxx;
        index  index.html index.htm index.php;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

   
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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 # if (!-e $request_filename) { #rewrite ^/(.*) /index.php?$1 last; rewrite ^(?!/favicon)(?!/data)(?!/static)(?!/MP)(.*)$ /index.php?$1 last; break; } location ~ \.php$ { root /data/wwwxx; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }

https-ssl帶證書配置站點 web.conf

server {
    listen       443;
    server_name  www.xx.cn ;

    if ($host = xx.cn){
        rewrite ^/(.*)$ https://www.xx.cn/$1 permanent;
    }

    ssl on;
    ssl_certificate /data/ssl/xx.cn.crt;
    ssl_certificate_key /data/ssl/xx.cn.key;

    #charset koi8-r;

    access_log  /data/logs/wwwxx.access.log  main;


        root   /data/wweb;
        index  index.html index.htm index.php;


    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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 / {
    if (!-e $request_filename) {
        #rewrite ^/(.*) /index.php?$1 last;

      rewrite ^(?!/pc)(?!/static)(?!/robots)(.*)$ /index.php?$1 last;

        break;
    }
  }

    location ~ \.php$ {
        root           /data/wweb;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apaches document root
    # concurs with nginxs one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

反向代理站點配置 two.conf

upstream two_web{
    server 127.0.0.1:8088 weight=1;
    server 127.0.0.1:8081 weight=2;
}


server {
    listen       80;
    server_name  www.xx.com;

    #charset koi8-r;

    access_log  /data/logs/xx.access.log  main;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        
        index  index.htm index.html;

        proxy_pass   http://two_web;    #在這裏設置一個代理,和upstream的名字一樣
        #以下是一些反向代理的配置可刪除
        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; 
        client_max_body_size       10m; #允許客戶端請求的最大單文件字節數
        client_body_buffer_size    128k; #緩沖區代理緩沖用戶端請求的最大字節數
        proxy_connect_timeout      300; #nginx跟後端服務器連接超時時間(代理連接超時)
        proxy_send_timeout         300; #後端服務器數據回傳時間(代理發送超時)
        proxy_read_timeout         300; #連接成功後,後端服務器響應時間(代理接收超時)
        proxy_buffer_size          4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
        proxy_buffers              4 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
        proxy_busy_buffers_size    64k; #高負荷下緩沖大小(proxy_buffers*2)
        proxy_temp_file_write_size 64k;
        

    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

  
}

靜態資源訪問配置 image.conf

server {
    listen       80;
    server_name  img002.xx.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;


    location / {
        root   /data/www/resource/;
        index  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

 
}

nginx多站點配置