在PHP编译安装那一节里,我们简单说了下安装PHP之后,Nginx的一些配置,这里给出一份较为详细的配置说明:
注意思考浏览器同源策略 SAMEORIGIN 和 server_tokens 的用途。提示: iframe
user nginx;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 204800;
events {
worker_connections 204800;
use epoll;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
server_tokens off; #隐藏Nginx版本号,避免敏感信息
keepalive_timeout 65;
gzip on; #开启GZIP
gzip_min_length 1k;
gzip_comp_level 2;
#这里类型自己加,具体略过 ..
gzip_types text/plain application/x-javasc .. application/json;
tcp_nodelay on;
#默认配置,略过 ..
log_format main '$remote_addr{|}$remote_user{|}$time_local{|} ..';
#思考这个浏览器同源策略 SAMEORIGIN 的用途
add_header X-Frame-Options SAMEORIGIN;
#conf.d目录将包含所有的server段的配置,比如 conf.d/yourhost.com.conf ..
include conf.d/*.conf;
}
注意思考,这里为什么这么奇怪的配置 server_name _,为什么要返回403?提示:你可以试试其他域名指过来主机头的效果就明白了。
server{
listen 80 default_server;
server_name _;
return 403;
}
server {
listen 80;
server_name yourhost.com #你的域名
root /Data/webapps/xxx;
location ~ /.svn/ { #禁止 svn 信息
deny all;
}
location ~ /.git/ { //禁止 git 信息
deny all;
}
location / {
index index.php index.html index.htm;
}
if (!-e $request_filename){ #urlrewrite 示例
#rewrite ^/(.*)\.html$ /$1.php;
}
fastcgi_intercept_errors on; #注意这样配置,PHP的错误才会走50X页面
error_page 500 502 503 504 = /50x.html; #50X错误
error_page 404 /404.html; #404页面
location ~ \.php$ {
#你选择的PHP处理方式
fastcgi_pass unix:/Data/apps/php-7.2.8/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}
#这些做本地缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d; #30d是30天
}
location ~ .*\.(js|css)?$
{
expires 10d;
}
access_log off; //可以不记录访问日志
error_log /Data/logs/youhost.error.log error; //错误日志
}
至此,我们的PHP环境已经搞定,按照惯例,第一个程序,让我们一起用 PHP 来 Say Hello World!
文件名假定是 hello.php
<?php echo "Hello World!"; //输出在PHP里用echo ?> //上面将输出 Hello World!
在浏览器里,比如 http://localhost/hello.php 来浏览查看输出,或者通过在命令行里,CLI 方式执行 PHP 查看运行结果(后面章节会介绍如何用 CLI 方式运行 PHP)。
这里给大家介绍一个代码在线运行工具,可以线上运行一些简单的PHP作为学习使用,地址如下(除了可以运行PHP,也可以运行C/C++、Python、Go、Java、NodeJS、Lua、Groovy、Bash..):