Nginx 配置文件在线生成: https://nginxconfig.io/

# Nginx的启动、停止与重启

  • 建立软连接Nginx/usr/bin目录下 ln -s /usr/sbin/nginx /usr/bin

# 启动

  • 启动代码格式:nginx安装目录地址 -c nginx配置文件地址
[root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 停止

nginx的停止有三种方式

从容停止

  • 查看进程号 ps -ef|grep nginx

  • 杀死进程 kill -QUIT 2072

快速停止

  • 查看进程号 ps -ef|grep nginx

  • 杀死进程 kill -TERM 2132 kill -INT 2132

  • 强制停止 pkill -9 nginx

重启

验证nginx配置文件是否正确

  • 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 看到如下显示nginx.conf syntax is ok nginx.conf test is successful说明配置文件正确

  • 方法二:在启动命令-c前加-t

重启Nginx服务

  • 方法一:进入nginx可执行目录sbin下,输入命令./nginx -s reload即可

  • 方法二:查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务

# Nginx基础配置

  • Nginx目录下的vhost或conf.d目录下新建一个配置文件(如poetries-80.conf
  • 把server的内容配置进去
  • Nginx.conf中的httpinclude配置文件
  • 检测配置文件是否出错 切换到/etc/nginx下 nginx -t
  • 重新加载配置文件 nginx -c /usr/local/etc/nginx/nginx.conf
  • 在重启Nginx nginx -s reload
user  root;  //Nginx需要有有一个用户
worker_processes  2; // Nginx进程数 最大1024
pid        conf/nginx.pid; 
worker_rlimit_nofile 2048;
events {
    use epoll;
    worker_connections  2048;
}

http {
 

	server {
       listen       80;
       server_name  119.29.145.252;
	   
       location / {
           root   /usr/local/nginx/html;
           index  index.html index.htm;
        }
   }
   server {
       listen       3001;
       server_name  119.29.145.252;
	   
       location / {
           root   /usr/local/nginx/book;
           index  index.html index.htm;
        }
   }
   server {
       listen       9000;
       server_name  119.29.145.252;
	   
       location / {
           root   /usr/local/nginx/vue;
           index  index.html index.htm;
        }
   }

}

# 一些错误

nginx: [error] invalid PID number “” in/usr/local/var/run/nginx/nginx.pid”
  • 解决办法:nginx -c /usr/local/etc/nginx/nginx.conf
  • nginx -s reload

权限问题导致Nginx 403 Forbidden错误的解决方法

  • nginx.conf头部加入一行 user root;
  • 重启nginx再访问,就可以正常访问了
阅读全文