一、前言
之前我们已经安装Nginx和PHP,以及完美的成功整合,今天我们讲解nginx的虚拟主机的如何配置
二、虚拟主机
1、虚拟主机配置
进入 /usr/local/nginx/conf
目录,创建vhost文件夹 mkdir vhost
进入 vhost 目录,创建default.conf,复制以下代码到该文件下:
server {
# Standard virtualhost
listen 9888;
#server_name localhost;
root "/home/www/laravel5.4";
index index.php;
charset utf-8;
sendfile off;
client_max_body_size 100m;
access_log off;
error_log /var/log/nginx/nginx-vhost.log error;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
# Pass to FastCGI
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
}
location ~ /\.ht {
deny all;
}
# Send requests to /socket.io to Node.js server
}
监听9888端口,默认访问根目录 /home/www/laravel5.4
2、修改nginx配置
添加 include vhost/*.conf
,加载虚拟主机配置文件。重启nginx nginx -s reload
查看监听状态
netstat -atunlp
说明虚拟主机端口监听成功
3、安全组规则
登陆阿里云控制台,添加安全组规则设置允许9888端口访问
4、访问虚拟主机端口
在 /home/www/laravel5.4 目录创建index.php
<?php
echo " Hello World ";
>
在浏览器 阿里云外网ip xx.xx.xx.xx:9888
wget 127.0.0.1:9888
curl 127.0.0.1:9888
linux本地访问提示502错误
window ping tcping64 xx.xx.xx.xx:9888
tcping安装教程
tcping64 xx.xx.xx.xx 9888
Probing xx.xx.xx.xx:9888/tcp - Port is open - time=29.194ms
Probing xx.xx.xx.xx:9888/tcp - Port is open - time=32.686ms
Probing xx.xx.xx.xx:9888/tcp - Port is open - time=28.986ms
Probing xx.xx.xx.xx:9888/tcp - Port is open - time=32.696ms
Ping statistics for xx.xx.xx.xx:9888
4 probes sent.
4 successful, 0 failed.
Approximate trip times in milli-seconds:
Minimum = 28.986ms, Maximum = 32.696ms, Average = 30.890ms
端口是打开的,但就是不能访问
5、问题解决
1、查看虚拟主机自定义的错误日志
error_log /var/log/nginx/nginx-vhost.log error;
2017/08/25 10:00:14 [crit] 23752#0: *134 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) upstream: "fastcgi://unix:/tmp/php-cgi.sock:"
出现以上问题请检查nginx和php-fpm的配置
nginx虚拟主机配置监听 fastcgi_pass unix:/tmp/php-cgi.sock
为 unix socket
方式;
php-fpm默认监听 listen = 127.0.0.1:9000
为 TCP socket
, 两者不一致
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000
其中fastcgi_pass为配置nginx与php-fpm的交互路径,一般有两种方式
sock方式:fastcgi_pass unix:/tmp/php-cgi.sock;
http方式:fastcgi_pass 127.0.0.1:9000;
任选其中一种即可,但必须和php-fpm的配置一致
这里我们使用 fastcgi_pass 127.0.0.1:9000
;
nginx重启后浏览器访问显示 “Hello World!”,so 配置成功!
三、TCP socket和unix socket
1、TCP和unix domain socket方式对比
其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景
- TCP是使用TCP端口连接127.0.0.1:9000
- Socket是使用unix domain socket连接套接字/dev/shm/php-cgi.sock(很多教程使用路径/tmp,而路径/dev/shm是个tmpfs,速度比磁盘快得多)
fastcgi_pass unix:/tmp/php-cgi.sock
fastcgi_pass 127.0.0.1:9000
理论上,unix socket 不走网络,效率高一些,但稳定性不是很理想,看这个测试 http://blog.csdn.net/liv2005/article/details/7741732
2、路径
UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPMTCP Socket(本地回环):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPMTCP Socket(Nginx和PHP-FPM位于不同服务器):
Nginx <=> socket <=> TCP/IP <=> 物理层 <=> 路由器 <=> 物理层 <=> TCP/IP <=> socket <=> PHP-FPM
4、mysql连接方式
像mysql命令行客户端连接mysqld服务也类似有这两种方式:
使用Unix Socket连接(默认):
mysql -uroot -p –protocol=socket –socket=/tmp/mysql.sock
使用TCP连接:
mysql -uroot -p –protocol=tcp –host=127.0.0.1 –port=3306