Nginx备忘
安装
- 准备
# 安装nginx
sudo yum install nginx
# 查看nginx版本
nginx -v
# 查看nginx配置文件目录
nginx -t
# 查看nginx在哪里
whereis nginx
# 授权
sudo chmod u+s nginx
# 或者(根据whereis nginx查询结果)
sudo chmod u+s /usr/sbin/nginx
运行
# 启动(默认配置)
nginx
# 启动(指定配置)
nginx -c ~/nginx/conf/nginx.conf
# 查看进程
ps -ef | grep nginx
# 重启
nginx -s reload
# 停止
nginx -s stop
# 关闭
nginx -s quit
配置
# nginx配置目录
cd /etc/nginx/conf.d
# 新建一个配置文件
sudo touch xkyii.cn.conf
# 所属权修改给用户xkyii
sudo chown xkyii xkyii.cn.conf
# 修改权限
sudo chmod 755 xkyii.cn.conf
启动一个静态网站
/etc/nginx/conf.d/xkyii.cn.conf
内容:
server {
listen 80;
# 域名
server_name 0.xkyii.cn;
# 网站文件目录
root /home/xkyii/www/0.xkyii.cn;
# 首页
index index.html;
}
- 域名增加A记录,指向服务器IP
- 将网站文件放置在
/home/xkyii/www/0.xkyii.cn
目录中 - 重启:
nginx -s reload
- 访问: 0.xkyii.cn
Win
安装
scoop install nginx
启动
cd D:\Scoop\apps\nginx\current
# 需要去任务管理器强制关闭进程
nginx
# 启动
start nginx
# 关闭
nginx -s stop
在其他目录启动会报错:
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2024/03/12 13:39:35 [emerg] 21228#9208: CreateFile() "C:\Users\dev88/conf/nginx.conf" failed (3: The system cannot find the path specified)
TCP代理
参考Assistant家族工具免费版绕过本机限制教程(Redis、ZooKeeper、Kafka等)
参考:
worker_processes 1;
events {
worker_connections 1024;
}
stream {
# TCP代{过}{滤}理清单
# 端口 服务
# 5555 虚拟机kafka集群
# 5556 10.100.11.33启动的kafka单机
# 5557 虚拟机rocketmq单机
# 5558 10.100.15.32启动rocketmq单机
server {
listen 5555;
ssl_preread on;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass 192.168.175.128:9092; # 本地虚拟机使用docker启动的kafka集群
}
server {
listen 5556;
ssl_preread on;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass 10.100.11.33:9092; # 11.33启动的单broker,有zookeeper,开启了sasl+acl认证
}
server {
listen 5557;
ssl_preread on;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass 192.168.175.128:9876; # 本地虚拟机使用docker启动的rocketmq单机
}
server {
listen 5558;
ssl_preread on;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass 10.100.15.32:9876; # 15.32启动的rocketmq单机
}
server {
listen 17777;
ssl_preread on;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass 114.116.242.27:17777;
}
}
问题
403 forbidden
- nginx是以root的身份安装的,但是
/etc/nginx/nginx.conf
中配置user nginx;
,其希望你以nginx
用户的身份来运行 - 解决方案: 修改
user nginx;
为user root