php nginx 路径批量配置

Posted mingzhanghui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php nginx 路径批量配置相关的知识,希望对你有一定的参考价值。

 * 假设 E:\\upload 作为图片上传的位置 nginx 做web服务

 

* 创建文件conf.php 放到这个目录下

<?php
function handleDir($it, &$a) {
	foreach ($it as $fileinfo) {	
		if ($fileinfo->isDir()) {
			$filename = $it->getFilename();
			if ($filename !== "." && $filename !== "..") {
				array_push($a, $filename);
			}
		}
	}
}

$it = new \\DirectoryIterator(".");

$baseDir = preg_replace(\'/\\\\\\\\/\', \'/\', __DIR__);

$filenames = [];
handleDir($it, $filenames);

$out = new SplFileObject($baseDir."/out.txt", "w+");

array_walk($filenames, function($value, $key, $dir) use ($out) {
	$path = $dir .\'/\'.$value;
	$out->fwrite("location /".$value." {".PHP_EOL);	
	$out->fwrite("    alias ".$path.";".PHP_EOL);
	$out->fwrite("    allow all;".PHP_EOL);
	$out->fwrite("    autoindex on;".PHP_EOL);
	$out->fwrite("    charset utf-8,gbk;".PHP_EOL);
	$out->fwrite("}".PHP_EOL);
}, $baseDir);

/*
		location /schoolhomepage {		
			alias E:/upload/schoolhomepage;
			allow all;
			autoindex on;
			charset utf-8,gbk;
		}
*/		

  

 * 把out.txt粘贴到 nginx 的conf路径下的nginx.conf文件中的对应位置

#user  nobody;
worker_processes  1;

#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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8089;
        server_name  localhost;
        #charset koi8-r;
        charset utf-8;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            charset utf-8;
        }

        #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;
        }
location /appPoster {
    alias E:/upload/appPoster;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /images {
    alias E:/upload/images;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /include {
    alias E:/upload/include;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /png {
    alias E:/upload/png;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /poster {
    alias E:/upload/poster;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /publish {
    alias E:/upload/publish;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /reporterActivity {
    alias E:/upload/reporterActivity;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /schoolhomepage {
    alias E:/upload/schoolhomepage;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /userAvatar {
    alias E:/upload/userAvatar;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /webPoster {
    alias E:/upload/webPoster;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}
location /writerActivity {
    alias E:/upload/writerActivity;
    allow all;
    autoindex on;
    charset utf-8,gbk;
}

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

}
nginx.conf

 

* 重启nginx服务

 

 

以上是关于php nginx 路径批量配置的主要内容,如果未能解决你的问题,请参考以下文章

PHP 网站如何修改默认访问文件的nginx配置

Nginx 配置php

nginx反向代理配置相对路径

如何在linux下配置nginx支持php

如何正确配置 Nginx + PHP

Nginx配置