一、
1.如果代理的应用过多,可以单独配置一个主文件这样方便管理
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /data/nginx_config/*.conf;
}
#include /data/nginx_config/*.conf;为每个应用的单独nginx配置文件路径,文件内只写server就行
2.单独配置文件内容:
server {
listen 11111;
server_name gwst_mange;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
break;
}
root /data/text//web;
index index.html;
}
location ^~/di-manage/ {
proxy_pass http://localhost:9995/di-manage/;
}
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded $proxy_add_x_forwarded_for;
}
二、隐藏ngin版本设置
要隐藏nginx版本信息,你需要编辑nginx的配置文件,通常位于 /etc/nginx/nginx.conf 或者 /usr/local/nginx/nginx.conf。在 http 块中添加 server_tokens off; 语句即可。这个设置会关闭nginx的版本信息显示,nginx配置文件以实际路径为准

三、nginx非root用户启动
首先有一个非root用户,没有需要创建一个用户
然后修改nginx.conf配置文件将user root修改为user 创建的用户
然后切换到创建的用户,重启nginx服务
四、-bash: nginx: 未找到命令 (command not found) 解决方案
1.查看是否安装成功
ps -ef | grep nginx
![]()
出现此图表示安装成功,否则需要安装nginx
2.修改环境变量
vim /etc/profile
3.生效配置
source /etc/profile
4.重启nginx
nginx -s reload
五、验证配置+启动+重载
#1. 验证配置文件(避免语法错误)
sudo /usr/local/nginx/sbin/nginx -t
2. 启动(验证通过后) sudo /usr/local/nginx/sbin/nginx
3. 修改配置后平滑重载 sudo /usr/local/nginx/sbin/nginx -s reload
六、设置开启启动
1. 创建系统服务文件 sudo vim /usr/lib/systemd/system/nginx.service
2. 粘贴以下内容(修改 ExecStart 为你的 Nginx 可执行路径)
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid # 对应你的 Nginx PID 路径ExecStart=/usr/local/nginx/sbin/nginx # 你的 Nginx 可执行文件路径ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.刷新系统服务并设置开机自启
sudo systemctl daemon-reloadsudo
systemctl enable nginx
Nginx配置与优化指南
本文介绍了如何通过配置多个代理应用的主文件、隐藏Nginx版本信息、使用非root用户启动Nginx以及解决`nginx:commandnotfound`问题的方法。
1514

被折叠的 条评论
为什么被折叠?



