nginx 实战配置

一、配置一个默认80端口的,静态页面,路径是path1。 http://192.168.0.111/path1 , /path1路径指向linux的/data/index1.html

vi /data/nginx-1.24.0/conf/nginx.conf 文件添加以下配置

location /path1 {

alias /data/; # 使用 alias 直接映射路径

index index1.html; # 指定默认访问文件

try_files uri uri/ /index.html; # 确保路径存在时返回正确文件

}

成功

二、配置php的一个测试页面保证可以用,端口是88,http://192.168.0.111:88/

这里用的是88端口,指向默认的页面

vi /data/nginx-1.24.0/conf/nginx.conf

配置如下:

server {

listen 88 default_server;

listen [::]:88 default_server;

这里改动了,也可以写你的域名

server_name localhost;

默认网站根目录(www目录)

root /www/html;

index php1.php index.html index.htm;

Load configuration files for the default server block.

include /data/nginx-1.24.0/conf*.conf;

location / {

这里改动了 定义首页索引文件的名称

index php1.php index.html index.htm;

}

error_page 404 /404.html;

location = /40x.html {

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

}

这里新加的

PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.

Fastcgi服务器和程序(PHP,Python)沟通的协议.

location ~ \.php$ {

root /www/html;

设置监听端口

fastcgi_pass unix:/data/php-8.0.24/php-cgi.sock;

设置nginx的默认首页文件(上面已经设置过了,可以删除)

fastcgi_index php1.php;

设置脚本文件请求的路径

fastcgi_param SCRIPT_FILENAME /www/html$fastcgi_script_name;

引入fastcgi的配置文件

include fastcgi_params;

}

}

保存 /data/nginx-1.24.0/conf/nginx.conf

makdir /html

cd /html

mkdir html

vi php1.php

保存

通过nginx 检查nginx的配置文件是否有语法错误,下面就是没有语法错误

重新启动nginx

防火墙开启88端口

sudo firewall-cmd --zone=public --add-port=88/tcp --permanent

sudo firewall-cmd --reload

访问页面测试

出现这个页面就是访问成功

期间可能会出现的错误,可以查看nginx的错误文件。

如果出现这个错误

crit\] 64685#0: \*10 connect() to unix:/data/php-8.0.24/php-cgi.sock failed (13: Permission denied) while connecting t o upstream, client: 192.168.0.103, server: localhost, request: "GET /php1/ HTTP/1.1", upstream: "fastcgi://unix:/data/php-8.0.24/php-cgi. sock:", host: "192.168.0.111" #### **1. 检查套接字文件权限与所有权** ``` ls -l /data/php-8.0.24/php-cgi.sock ``` # 修改套接字文件权限(允许Nginx用户或组访问) sudo chmod 660 /data/php-8.0.24/php-cgi.sock # 修改套接字文件所属组(确保与Nginx进程组一致) sudo chown leo:leo /data/php-8.0.24/php-cgi.sock # 修改目录权限(确保Nginx有权访问目录) sudo chmod 755 /data/php-8.0.24/ #### **2、确认PHP-FPM与Nginx用户/组一致性** ![](https://i-blog.csdnimg.cn/direct/ffe95dc0dd574ed28c9618840a36cab3.png) 我这个是一制的,都是一样的用户启动php-fpm,nginx ##### **查看PHP-FPM配置** ![](https://i-blog.csdnimg.cn/direct/e2fe292f31664d749b56abb0869a541e.png) 修改配置。 # 重启PHP-FPM(确保套接字文件重新生成) sudo systemctl restart php-fpm # 重启Nginx sudo systemctl restart nginx **使nginx支持https** 查看是否安装了ssl,如果没有--with-http_ssl_module,说明未安装ssl ![](https://i-blog.csdnimg.cn/direct/c638fe94c0674defa13cbfd861bfc96b.png) 解决办法,重新编译 原来的修改一下名字,备份 cd /data sudo systemctl stop nginx sudo systemctl status nginx mv nginx-1.24.0/ nginx ![](https://i-blog.csdnimg.cn/direct/14eafd41067748b79a265cd9790f5208.png) 解压 cd sudo cp /software/nginx-1.24.0.tar /data/nginx-1.24.0.tar sudo tar -xvf nginx-1.24.0.tar sudo mv nginx-1.24.0 nginx1 cd nginx1 ./configure --prefix=/data/nginx-1.24.0 --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre sudo make sudo make install 完成后查看当前版本有没有htts功能 cd /software/nginx-1.24.0/sbin/ sudo ./nginx -V `nginx -V`(注意是大写的V) ![](https://i-blog.csdnimg.cn/direct/b1bce05dc92f4815aaaca26793df0b06.png) 把原来的配置拷贝过来 cd /data/nginx-1.24.0/conf/ mv nginx.conf nginx.conf.bak cp /data/nginx1/conf/nginx.conf /data/nginx-1.24.0/conf/nginx.conf sudo systemctl start nginx sudo systemctl status nginx ![](https://i-blog.csdnimg.cn/direct/564a1db8337a4fa598373425ebb942c7.png) 原来的配置也是生效的 ![](https://i-blog.csdnimg.cn/direct/138bfe6de5694ab8b936f39eebb8e849.png)

相关推荐
IT成长日记2 小时前
【Docker基础】Docker数据持久化与卷(Volume)介绍
运维·docker·容器·数据持久化·volume·
物联网老王4 小时前
Ubuntu Linux Cursor 安装与使用一
linux·运维·ubuntu
艾伦_耶格宇5 小时前
【ACP】阿里云云计算高级运维工程师--ACP
运维·阿里云·云计算
一位摩羯座DBA6 小时前
Redhat&Centos挂载镜像
linux·运维·centos
charlee446 小时前
nginx部署发布Vite项目
nginx·性能优化·https·部署·vite
cui_win7 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
风清再凯7 小时前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
深圳安锐科技有限公司7 小时前
深圳安锐科技发布国内首款4G 索力仪!让斜拉桥索力自动化监测更精准高效
运维·安全·自动化·自动化监测·人工监测·桥梁监测·索力监测
猫头虎7 小时前
猫头虎 AI工具分享:一个网页抓取、结构化数据提取、网页爬取、浏览器自动化操作工具:Hyperbrowser MCP
运维·人工智能·gpt·开源·自动化·文心一言·ai编程
CRMEB定制开发8 小时前
【实战】CRMEB Pro 企业版安装教程(附 Nginx 反向代理配置 + 常见问题解决)
nginx·商城系统·微信商城·crmeb