且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[LNMP]Nginx解析php与代理

更新时间:2022-01-05 04:46:22

PHP解析

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@plinuxos ~]# vi /usr/local/nginx/conf/vhost/default.conf
server
{
    listen 80 default_server;  
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
    access_log /tmp/default.log juispan;
    location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;  ##用来指定php-fpm监听的地址或者socket
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
    }
}

要确保fcgi.sock路径存在:astcgi_pass unix:/tmp/php-fcgi.sock;路径不对,访问错误会报502错误。

2、检查与重载

1
2
3
4
[root@plinuxos ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@plinuxos ~]# /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
9
10
11
[root@plinuxos ~]# vi /data/wwwroot/default/1.php
<?php
phpinfo();
?>
[root@plinuxos ~]# curl -x127.0.0.1:80 aaa.com/1.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 15 Aug 2017 00:55:39 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30   ##php 5.6.30


Nginx代理

[LNMP]Nginx解析php与代理

1、创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@plinuxos ~]# cd /usr/local/nginx/conf/vhost
[root@plinuxos vhost]# vi /usr/local/nginx/conf/vhost/proxy.conf
server
{
   listen 80;
   server_name baidu.com;
   location /
   {
       proxy_pass http://111.13.101.208/;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

2、检查与重载

1
2
3
4
[root@plinuxos ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@plinuxos ~]# /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
[root@plinuxos vhost]# curl -x127.0.0.1:80 baidu.com
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>














本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1956295,如需转载请自行联系原作者