我初学Nginx服务器配置,有些操作记不住,所以总结一下放一起。根据学习使用的情况,内容会不定期更新。
0.环境说明
服务器1:腾讯云 CentOS7.3
服务器2:树莓派3B CentOS-Userland-7-armv7hl
腾讯云上的Nginx是使用yum安装的Nginx
树莓派上的Nginx是使用编译安装的方式安装的
1.Nginx安装
对于云服务器,在系统开机后可通过yum update
来更新yum数据,之后使用命令yum install nginx
来安装Nginx。
对于树莓派上的Nginx,yum的方式不能(我不会)安装Nginx,所以我采用编译安装的方式安装,安装方式见链接:http://blog.csdn.net/qq_36731677/article/details/78762902
2.Nginx安装目录
yum安装Nginx的配置文件目录:/etc/nginx/
编译安装的Nginx的配置文件目录:/usr/local/nginx/conf/
3.Nginx配置文件
无论是如何安装的Nginx,配置文件的修改方式都是一样的,只需要在每次修改配置文件后重启Nginx即可使用新的配置文件运行。
3.1 Nginx配置结构
http {
xxxxx
...
xxxxx
server {
listen 80;
server_name xxxxxx xxxxxx;
#include /etc/nginx/default.d/*.conf;
location xxxx{
xxxxx
...
xxxxx
}
error_page 500 502 503 504 /50x.html;
}
}
3.2 Nginx代理html
location / {
root /home/www;
index index.html index.htm;
}
3.3 Nginx路径代理(多路径)
location /abc/ {
alias /home/www/def/;
}
3.4 Nginx代理站点
location / {
proxy_set_header User-Agent "example.com";
proxy_pass http://test.com;
}
3.5 HTTP403重定向
location / {
rewrite ^/(.*)$ http://www.baidu.com/$1 permanent;
}
3.6 Nginx错误页
error_page 404 /404.html;
location = /40x.html {
root /home/www/err
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/www/err;
}
3.7 Nginx代理PHP7
需要开启php-fpm
编译安装的Nginx开启方式:/usr/local/php/sbin/php-fpm
(编译安装的Nginx自带fpm模块)
yum安装的Nginx开启php-fpm方式:systemctl start php-fpm
(fpm模块安装使用命令yum install php70w-fpm
)
apt-get安装的Nginx开启php-fpm方式:/etc/init.d/php7-fpm start
(需要安装php7.0-fpm
插件)
修改php-fpm与Nginx的连接方式,tcp模式或者socket模式。
首先要找到php-fpm的www.conf文件,这个文件不好找,不同的平台会导致文件位置不同。
找到参数listen = /run/php/php7.0-fpm.sock
(socket模式)或listen = 127.0.0.1:9000
(tcp模式)
最后根据模式的不同配置Nginx
location ~\.php$ {
root /home/www;
#fastcgi_pass /run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
我懂的操作总结完毕,下班睡觉去。