CentOS6.5上nginx通过location实现类似UserDir以及虚拟主机功能
搭建网站时,除了网站本身,我们还需要必要的配套服务,比如通过phpmyadmin来管理我们的网站数据库。
如果是Apache服务器,我们通过虚拟主机或者UserDir,可以很简单的实现上述要求,
在Nginx,没有虚拟主机与UserDir的概念,但这些功能可以通过Location匹配来实现。
注意,centos6.5的nginx的配置文件在/etc/nginx/nginx.conf,它包含引进所有/etc/nginx/conf.d/*.conf文件
1. 实现UserDir功能
假设我们有域名 qiais.com, 我们的网站主程序正在使用此域名,
另外,我们还想通过 http://qiais.com/phpmyadmin 来管理我们的数据库
我们可以通过以下配置来实现
# vi /etc/nginx/conf.d/default.conf
# # The default server # server { listen 80 default_server; server_name qiais.com; #charset koi8-r; #access_log logs/host.access.log main; location / { #root /usr/share/nginx/html; root /var/www/html/html; index index.php index.html index.htm; auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/conf.d/.htpasswd; } error_page 404 /404.html; location = /404.html { #root /usr/share/nginx/html; root /var/www/html/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; root /var/www/html/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 /var/www/html/html; 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; #} location /phpmyadmin { root /home/sai/public_html; index index.php index.html index.htm; location ~ ^/phpmyadmin/(.+\.php)$ { try_files $uri =404; root /home/sai/public_html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /home/sai/public_html; } }
location / {
#root /usr/share/nginx/html;
root /var/www/html/html;
index index.php index.html index.htm;
auth_basic “Restricted Area”;
auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
}
这段代码是配置我们主网站用的:http://qiais.com
location /phpmyadmin {
root /home/sai/public_html;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /home/sai/public_html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /home/sai/public_html;
}
}
这段代码是配置我们phpmyadmin用的:http://qiais.com/phpmyadmin,
当然,你需要将phpmyadmin的文件上传到/home/sai/public_html/phpmyadmin文件夹中。
2. 实现类似与虚拟主机的功能
这个很简单,只要从上述default.conf文件拷贝成新的配置文件,修改其中的server_name就可以了。
# cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/phpmyadmin.conf # vi /etc/nginx/conf.d/phpmyadmin.conf
# # The default server # server { listen 80; server_name phpmyadmin.qiais.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root /home/sai/public_html/phpmyadmin; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /home/sai/public_html/phpmyadmin; } # 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; root /home/sai/public_html/phpmyadmin; } # 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 /home/sai/public_html/phpmyadmin; 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; #} }
这样,访问 http://phpmyadmin.qiais.com 同样可以看到我们数据库管理界面。