# Nginx配置管理
[TOC]
初始化的nginx默认主配置
执行 `grep -Evi "#|^$" nginx.conf` 得到如下结果!
~~~
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
~~~
## nginx的配置段
### 全局配置区
`worker_processes 1;` 指有一个工作的子进程,可以自行修改,因为要争CPU资源,一般设置为CPU*核数。
### Event配置区
一般是配置Nginx连接的特性
例如:一个worker能同时允许产生多少个连接
` worker_connections 1024;` 配置值的时候需要结合系统参数。
### http服务器配置区
#### server虚拟主机配置区
>[danger] **提 示 :**如果未明确配置服务器IP地址访问配置,将使用第一个server配置。
##### 基于域名的虚拟主机
使用域名`www.domain.com`为例!
~~~
server {
listen 80;
server_name www.domain.com;
error_page 404 /404.html;
location / {
root html/www;
index index.html;
access_log logs/www.domain.com_access.log main;
error_log logs/www.domain.com_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~
* * * * *
##### 基于端口的虚拟主机
以端口8080为例,域名依然使用`www.domain.com`
~~~
server {
listen 8080;
server_name www.domain.com;
error_page 404 /404.html;
location / {
root html/port;
index index.html;
access_log logs/www.domain.com_access.log main;
error_log logs/www.domain.com_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~
* * * * *
##### 基于IP的虚拟主机
以`192.168.0.200`为默认Ip配置虚拟主机
~~~
server {
listen 80;
server_name 192.168.0.200;
error_page 404 /404.html;
location / {
root html/ipvhosts;
index index.html;
access_log logs/192.168.0.200_access.log mian;
error_log logs/192.168.0.200_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~