一、Nginx 服务的搭建与配置
1、系统环境
●OS: CentOS Linux release 7.5.1804 (Core)
2、关闭selinux
1.修改selinux的配置文件
[root@qfedu.com ~]# vim /etc/selinux/config
●将内容中的SELINUX=enforcing修改为SELINUX=disabled
2.关闭selinux
[root@qfedu.com ~]# setenforce 0
[root@qfedu.com ~]# reboot
[root@qfedu.com ~]# sestatus
SELinux status:disabled !
3、关闭防火墙
[root@qfedu.com ~]# systemctl stop firewalld.service
4、安装epel源
[root@qfedu.com ~]# yum -y install epel- release.noarch
5、设置nginx安装源
1.安装官方提供centos安装nginx源
[root@qfedu.com ~]# rpm -Uvh
http://nginx.org/packages/centos/7/noarch/RPM5/nginx-release-centos-0.e17ngx.noarch.rpm
2.手动添加nginx安装源
[root@qfedu.com ~]# vim /etc/yum.repos.d/ngjnx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/sbasearch/
gpgcheck=0
enabled=1
6、安装Nginx
●通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx.
[root@qfedu.com ~]# yum install -y nginx
7、启动Nginx并设置开机自动运行
[root@qfedu.com ~]# systemctl start nginx.service
[root@qfedu.com ~]# systemctl enable nginx.service
8、配置Nginx
1、Nginx 的主配置文件
[root@qfedu.com ~]# vim /etc/nginx/nginx.conf
2、Nginx 配置支持PHP
●在/etc/nginx/conf.d 目录下存放着多个配置文件这些配置项会在Nginx运行时加载到主配置项目
中(类似虚拟机)
●Nginx是通过php-fpm来通讯的 ,所以需要监听9000端口.
●在这个目录下生成一个自己的配置文件例如admin.conf
[root@qfedu.com ~]# vim /etc/nginx/conf.d/admin.conf
server {
listen 80; #端口
server_name www.test.com admin.test.com; #城名
root /var/www/card/public; #网站根目录
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last; #主要配置隐藏url中index.php
break;
}
}
location ~ \.php$ {
#root /var/www/card/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@qfedu.com ~]#yum -y install php-fpm
[root@qfedu.com ~]#vim /etc/php-fpm.d/www.conf
默认使用apache,我们修改为nginx。
3、Nginx 配置反向代理
cd /etc/nginx/conf.d/
upstream test{
server 192.168.1.15:8080 weight=1;
server 192.168.1.15:8080 weight=1;
}
server {
listen 80;
server.name porxy.zxy.com;
location / {
proxy_pass http://test; #这里可直接写IP地址进行配置如果需要配置负载均衡可以使用http://test和upstream名称一致
#以下是一些反向代理的配置可删除
proxy_redirect off;
proxy_set_header Host $host; #指定请求的服务器的域名和端口号:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for; #后端的web服务器可以通过x-Forwarded-For获取用户真实IP
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k;#缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 300;#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 300;#后端服务器数据回传时间(代理发送超时)