nginx 配置
_____________________________________________________设权限
sudo chmod -R 777 admin
_____________________________________________________定时crontab -e
*/1 * * * * sudo chmod -R 777 /phpstudy/admin
_____________________________________________________.htaccess
# nginx rewrite rule
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?/$1 break;
}
# end nginx rewrite rule
——————————————————————.htaccess
# nginx rewrite rule
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?/$1 break;
}
# end nginx rewrite rule
______________________________________________
/phpstudy/server/nginx/sbin/nginx -s reload nginx重启
service apache restart apache重启
/phpstudy/server/httpd/bin/apachectl restart apache重启
/phpstudy/server/httpd/bin/apachectl start apache启动
——————————————————
#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 80;
server_name x.com;
#charset koi8-r;
#access_log logs/host.access.log main;
root /phpstudy/admin/ych;
rewrite ^/(\d+)\.html$ /detail.php?id=$1 last;
include /phpstudy/admin/ych/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
#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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
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 admin.x.com;
#charset koi8-r;
#access_log logs/host.access.log main;
root /phpstudy/admin/ych_admin;
location / {
index index.html index.htm index.php;
}
#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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include /phpstudy/server/nginx/conf/vhosts/*.conf;
}
______________________________生成新站
/**
* test
*/
public function test() {
define("ROOT",dirname(dirname(dirname(dirname(__FILE__)))));
define("BROOT",dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))));
// echo "<pre>";var_dump(ROOT);exit;
// $lin=shell_exec('');
// $test = "sudo chmod -R 777 /phpstudy/admin/ych4"; //ls是linux下的查目录,文件的命令
// system($test); //执行命令
// print_r($array);
echo exec('sudo chmod -R 777 /phpstudy/admin/ych4');
echo ROOT."______________";
echo BROOT;
$rs=file_get_contents("root",BROOT."/var/spool/mail/");
var_dump($rs);
// $output = exec('sudo chmod -R 777 /phpstudy/admin/ych4');
// echo "<pre>$output</pre>";
// @touch('./install.lock');
//echo "<pre>";var_dump($_SERVER);exit;
if($_POST){
$filepath=$_POST['filepath'];
$site_name=$_POST['site_name'];
$site_name_s="admin.".$site_name;
$dirname=$_POST['dirname'];
$filepath_s=str_replace($dirname,$dirname."_admin",$filepath);
$filename = '/phpstudy/server/nginx/conf/vhosts/phpstudy.conf';
$somecontent = '
server {
listen 80;
server_name '.$site_name.';
#access_log logs/host.access.log main;
root '.$filepath.';
include /phpstudy/admin/'.$dirname.'/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}';
$somecontent2 = '
server {
listen 80;
server_name '.$site_name_s.';
#access_log logs/host.access.log main;
root '.$filepath_s.';
include /phpstudy/admin/'.$dirname.'/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}';
if (file_exists ( $filename )) {
file_put_contents($filename,$somecontent, FILE_APPEND);
}
if (!$handle = fopen($filename, 'a')) {
echo "不能打开文件 $filename";
exit;
}
copy_dir(ROOT."/ych",ROOT."/".$dirname);
echo "_____step1";
fclose($handle);
$dirname_s=$dirname."_admin";
copy_dir(ROOT."/ych_admin",ROOT."/".$dirname_s);
echo "_____step2";
file_put_contents($filename,$somecontent2, FILE_APPEND);
echo "<script>alert('success!!');history.go(-1);</script>";
}
echo <<<'EOD'
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<center>
<form method="post">
<table border="1" width="500">
<tr>
<td>
网址:
</td>
<td>
<input type="text" name="site_name" style="width: 300px" value="xx3.com"/>
</td>
</tr>
<tr>
<td>
filepath:
</td>
<td>
<input type="text" name="filepath" style="width: 300px" value="/phpstudy/admin/ych3/webroot"/>
</td>
</tr>
<tr>
<td>
dirname:
</td>
<td>
<input type="text" name="dirname" style="width: 300px" value="ych3"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit"/>
<a href="index.php"> <input type="button" value="reback"/></a>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
EOD;
// $this->display('user/branch_list', $data);
}
————————————多站点
server {
listen 80;
server_name admin.x.com;
#charset koi8-r;
#access_log logs/host.access.log main;
root /phpstudy/admin/ych_admin/webroot;
include /phpstudy/admin/ych_admin/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
#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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
server {
listen 80;
server_name xx8.com;
#access_log logs/host.access.log main;
root /phpstudy/admin/ych8/webroot;
include /phpstudy/admin/ych8/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
server {
listen 80;
server_name admin.xx8.com;
#access_log logs/host.access.log main;
root /phpstudy/admin/ych8_admin/webroot;
include /phpstudy/admin/ych8/webroot/.htaccess;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
__________________________设置Nginx作为缓存
完整的配置文件nginx.conf
- #user nobody;
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- 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 logs/access.log main;
- #source site ip and port
- upstream contactpool{
- server 106.38.193.183:80;
- }
- #proxy cache info
- 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 /home/temp_dir;
- proxy_cache_path /home/cache
- levels=1:2
- keys_zone=cache_one:50m
- inactive=20m
- max_size=30g;
- server {
- listen 80;
- server_name localhost;
- location / {
- #proxy cache info
- index index.html index.htm index.php;
- proxy_cache cache_one;
- proxy_cache_valid 200 302 1h;
- proxy_cache_key $host$uri$is_args$args;
- proxy_pass http://contactpool;
- proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; # 不处理后端服务器返回的指定响应头
- expires 30d;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
- }
1.2 测试
curl www.guowenyan.cn/test.html
用户第一次请求一个url,nginx会回源,将结果返回给用户的同时,把结果缓存;
第二次再请求该url时,nginx直接将结果返回给用户,无需回源。
是否回源,我是通过在nginx上抓包看的。