
Nginx相关安装、功能开放(主配置文件修改)、状态统计、访问控制虚拟主机
二、熟悉 Nginx 服务的主配置文件
发布日期:2021-05-08 20:55:06
浏览次数:23
分类:精选文章
本文共 3166 字,大约阅读时间需要 10 分钟。
Nginx 网站服务指南
一、编译安装 Nginx
1. 导入安装包
将 Nginx 安装包放置在 /opt
目录下。
2. 关闭防火墙
执行以下命令关闭防火墙:
systemctl stop firewalld && systemctl disable firewalld && setenforce 0
3. 安装依赖包
安装必要的软件包:
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
4. 创建程序用户及组
创建专用用户账号:
useradd -M -s /sbin/nologin nginx
5. 编译安装 Nginx
进入安装目录并配置:
cd /opttar zxvf nginx-1.12.0.tar.gz -C /usr/local/nginxcd /usr/local/nginx./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_modulemake -j 4 && make install
6. 查看 Nginx 进程号
几种方法查询 Nginx 进程号:
cat /usr/local/nginx/logs/nginx.pidnetstat -natp | grep nginxlsof -i :80ps -ef | grep nginx
7. 检查、启动、重启、停止 Nginx 服务
nginx -t # 检查配置nginx # 启动cat /usr/local/nginx/logs/nginx.pid # 查看 PIDkill -3# 停止kill -3 -s QUIT # 平滑停止make -l # 列出所有 Nginx 进程
8. 添加 Nginx 系统服务
创建服务脚本:
vim /etc/init.d/nginxchmod +x /etc/init.d/nginxchkconfig --add nginxsystemctl stop nginx && systemctl start nginx
二、熟悉 Nginx 服务的主配置文件 nginx.conf
1. 全局配置
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;keepalive_timeout 65;
2. I/O 事件配置
events { use epoll; worker_connections 4096;}
3. HTTP 配置
http { server { listen 80; server_name www.kgc.com; charset utf-8; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
4. 地址匹配配置
location /tt { root /usr/local/nginx/html/tt; index index.html;}location / { root html; index index.html index.htm;}
5. 别名配置
server { listen 80; server_name example.com; location / { root /var/www/html; index index.html; } location /admin { alias /var/www/html/admin; index index.html; }}
6. 访问状态统计
location /status { stub_status on; access_log off;}
三、基于授权的访问控制
1. 生成密码认证文件
htpasswd -c /usr/local/nginx/passwd.db
2. 修改主配置文件
auth_basic "secret";auth_basic_user_file /usr/local/nginx/passwd.db;
3. 测试访问
nginx -t && systemctl restart nginx
四、基于客户端的访问控制
1. 修改主配置文件
deny 192.168.40.30;allow all;
2. 测试访问
访问 http://192.168.40.20
时会被拒绝。
五、虚拟主机配置
1. 基于域名的虚拟主机
server { listen 80; server_name www.rain.com; charset utf-8; location / { root /var/www/html/rain; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
2. 基于 IP 的虚拟主机
server { listen 192.168.40.20:80; server_name www.rain.com; charset utf-8; location / { root /var/www/html/rain; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
3. 基于端口的虚拟主机
server { listen 192.168.40.20:8080; server_name www.rain.com; charset utf-8; location / { root /var/www/html/rain; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月18日 01时37分26秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
2019-03-06
Problem 330A - Cakeminator (思维)
2019-03-06
LeetCode75 颜色分类 (三路快排C++实现与应用)
2019-03-06
C语言+easyX图形库的推箱子实现
2019-03-06
调试vs2019代码的流程
2019-03-06
脱壳与加壳-加壳-6-代码实现加密导入表
2019-03-06
Typora配置PicGo时,提示Failed to fetch
2019-03-06
bcolz的新操作
2019-03-06
zmq的send
2019-03-06
阿里钉钉面试题
2019-03-06
C++中找资源或者函数的方法
2019-03-06
delete对象时会自动调用类的析构函数
2019-03-06
POD类型
2019-03-06
const与常量,傻傻分不清楚~
2019-03-06
Head First设计模式——迭代器模式
2019-03-06
MongoDB版本及存储引擎区别
2019-03-06
shell echo单行和多行文字定向写入到文件中
2019-03-06
cmp命令
2019-03-06
Linux 磁盘管理(df fu fdisk mkfs mount)
2019-03-06
jQuery的事件绑定与触发 - 学习笔记
2019-03-06