Nginx 配置虚拟主机

1. 当前nginx的版本

[root@localhost ~]# nginx -v
nginx version: nginx/1.10.3
2. 查看nginx 目录结构
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# tree ../nginx
../nginx
├── conf.d
│   ├── default.conf
│   └── default.conf.rpmsave
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

3.检查/etc/nginx/nginx.conf配置文件,确保文件中有:include /etc/nginx/conf.d/*.conf; 例如:

[root@localhost nginx]# cat nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/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 /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

4. 在目录/etc/nginx/conf.d/下面新建文件conf文件,文件名可以任意写,自己看明白就OK,但是后缀名为.conf。
第一个,默认的为default.conf,我们让默认的使用80端口

[root@localhost nginx]# cat conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
root /data/www/blog;                                                                        #网站根目录
index index.php index.html index.htm;                                          #支持的index的类型
}

#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 /data/www/blog;                                                                    #错误目录
}

# 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 /data/www/blog/$fastcgi_script_name;                              #根目录+$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;
#}
}

 

然后我们可以cp一个,并且命名为其他的名字如:

cp default.conf test.conf

然后再次编辑test.conf 修改主目录,监听的端口等。

然后保存配置,重启nginx

 service nginx restart 

 

参考地址:
http://blog.openlg.net/index.php/archives/266
https://ninghao.net/blog/1368