Nginx踩坑实战

Nginx启动

通过配置文件启动,其中-c是配置文件的意思

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

若是将nginx添加到环境变量中,那么直接输入nginx即可启动

先vim /etc/profile

然后在文件中写

PATH=$PATH:/usr/local/nginx/sbin
export PATH

如果输入该命令,结果报错,那么可能是端口占用引起的,可以先查看是否已有nginx在运行,有的话就kill掉进程


 

启动之后,如何查看运行状态

ps -ef | grep nginx


 

查看之后有两个进程,一个master 一个worker,说明nginx启动好了

nginx: master process

nginx: worker process

其中:

master进程用来读取配置文件并且维护worker进程

worker进程用来对请求进行实际处理


关闭Nginx

kill -QUIT masterPID  (其中masterPID为主进程的PID,用ps -ef | grep nginx查找) 会执行完当前请求再关闭进程

 

service nginx stop

nginx -s stop

 

重启Nginx 在sbin目录下

./nginx -s reload


配置检查

nginx -t

查看版本

nginx -v 或者 nginx -V,后者更详细些

查看日志

 

cat /var/log/nginx/error.log

 

启动关闭nginx

service nginx start

service nginx stop

systemctl start nginx

systemctl stop nginx

 

部署vue跑起来报403的话

可以改nginx.conf 中的 user改成和master一样的 一般是 user root

 


 

 

反向代理配置的坑

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

如果都对这个url http://ip:port/proxy/test.html进行访问,结果如下 

 

第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html

 

第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html

 

第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html

 

第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html

 

posted @ 2020-03-18 23:32  Tudoo  阅读(164)  评论(0编辑  收藏  举报