nginx 配置文件配置多個站點

nginx環境下配置多個站點php

 

1、找到nginx的配置文件 通常會在/etc/nginx/nginx.confhtml

2、修改配置文件個人配置文件以下,其中server 包括的內容是一個對象 一個網站對應一個server 多個網站對應多個servernginx


#user  nobody;
worker_processes  1;
web

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

#pid        logs/nginx.pid;
events {
    worker_connections  10240;
    multi_accept        on;
    use                 epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;app

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';tcp

    #access_log  logs/access.log  main;網站

    sendfile        on;
    #tcp_nopush     on;ui

    #keepalive_timeout  0;
    keepalive_timeout  65;spa

    #gzip  on;

  //第一個站點配置 看關鍵點(域名+定位到的項目地址)

    server {
        listen       80;
        server_name  localhost;     //重要 你的域名
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /web/men/public;  //定位到的項目地址
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
            }
        }


    location ~ \.php?.* {
            root           /web/men/public; //定位到的項目地址
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            set $real_script_name $1;
            set $path_info $2;
            }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
           }       


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

 //第二個站點配置如第一配置同樣只是須要改變域名和定位到的項目地址
         server {
        listen       80;
        server_name  ruigao.cqgoulian.com;     
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /web/ruigao/public;
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
            }
        }


    location ~ \.php?.* {
            root           /web/ruigao/public;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            set $real_script_name $1;
            set $path_info $2;
            }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
           }       


        #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;
    #    }
   }
3、重啓nginx 輸入命令行 # service nginx restart 提示OK則表明成功 提示警告內容則須要修改問題

4、打開遊覽器訪問不一樣站點的域名 發現定位到不一樣的項目地址 成功!

相關文章
相關標籤/搜索