nginx安装及使用
Nginx (engine x) 是一个高性能web服务器和反向代理服务器。
官网: https://nginx.org/en/download.html
安装
# 在Debian10上安装nginx
apt install nginx
# 查看版本
nginx -v
# nginx version: nginx/1.14.2
# 启动 /usr/sbin/nginx
nginx
# 指定配置文件启动
nginx -c /etc/nginx/nginx.conf
# 查看进程
ps aux | grep nginx
# 查看状态
systemctl status nginx
# 重启nginx
systemctl restart nginx
# 停止
nginx -s stop
启动后可以用浏览器访问http://127.0.0.1
或服务器IP地址,展示默认的Welcome to nginx!
页面。
配置
默认配置文件路径: /etc/nginx/nginx.conf
Nginx作为Web服务器
1.创建网站内容
创建简单的HTML文件index.html
,放在/var/www/html
目录下
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>My Static Website</title>
</head>
<body>
<h1>Welcome to my website!</h1>
<p>This is a static website hosted by Nginx.</p>
</body>
</html>
2.配置Nginx服务
修改Nginx的配置文件vi /etc/nginx/nginx.conf
http {
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
# Other configurations here
}
重启nginx systemctl restart nginx
,浏览器重新访问服务器的IP地址即可看到网页内容。
Nginx作为反向代理服务器
http {
# 应用服务器
upstream app_service {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 代理访问地址: localhost:80
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app_service;
}
}
# Other configurations here
}
请求到localhost:80
地址的消息将会转发到app_service
中配置的后端服务器集群上。
如果未转发到后端服务器上,先检查下默认的配置有没有注释掉,比如
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析