新版本nginx的配置文件被拆分为若干部分
1、主配置文件为nginx.conf
2、与php相关的是fastcgi_params
3、与python相关的是uwsgi_params
4、…其他配置文件
[PS:首先确保占用80端口的服务被停止,nginx默认监听端口为80]
我们首先可以打开nginx.conf,命令如下:
D:\phpStudy\nginx\conf>nginx.conf
与Apache相同,nginx也可以配置多种类型的虚拟主机。
- 基于域名的虚拟主机
- 基于IP的虚拟主机
- 基于端口的虚拟主机
我们可以发现要配多个虚拟主机基本的格式是
server{……}
server{……}
虚拟主机建立的方式共分为三种:基于IP的虚拟主机,基于端口的虚拟主机和基于名称的虚拟主机。前两种由于受到成本和客户使用习惯的限制,相对使用的没有基于名称的虚拟主机多。
基于主机名称的虚拟机配置
nginx.conf:
server{
listen 8787;
server_name www.example.com;
root "D:/phpStudy/WWW/tp5/public";
# 匹配以/apis/开头的请求
location ^~ /apis/ {
proxy_pass https://api.shenjian.io/constellation/today;
}
location / {
index index.html index.htm index.php l.php;
autoindex on;
}
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;
}
}
hosts文件:
127.0.0.1 www.example.com
基于IP的虚拟主机配置:
server{
listen 127.0.0.1:8989;
server_name 127.0.0.1;
root "D:/phpStudy/WWW/tp5/public";
# 匹配以/apis/开头的请求
location ^~ /apis/ {
proxy_pass https://api.shenjian.io/constellation/today;
}
location / {
index index.html index.htm index.php l.php;
autoindex on;
}
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 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
root "D:/phpStudy/WWW";
# 匹配以/apis/开头的请求
location ^~ /apis/ {
proxy_pass https://api.shenjian.io/constellation/today;
}
location / {
index index.html index.htm index.php l.php;
autoindex on;
}
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;
}
# 实现访问控制
# loaction / {
# deny all;
# allow 45.76.202.231;
# }
# 复杂访问控制权限匹配, = 代表精确匹配
location =/img{
allow all;
}
location =/admin{
deny all;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}