初学Nginx服务器搭建,需要记住的操作命令及配置

348 阅读1分钟

启动nginx

  • nginx
  • Linux命令 systemctl start nginx.service
  • service nginx start

停止nginx

  • nginx -s stop 立即停止
  • nginx -s quit 当前工作完成后停止
  • killall nginx 杀死nginx进程
  • systemctl stop nginx.service Linux命令停止
  • service nginx stop

查看nginx是否启动

  • ps -ef | grep nginx

查看服务的运行情况

  • ps aux | grep nginx

重启nginx服务

  • systemctl restart nginx.service
  • service nginx restart

重载配置文件

  • nginx -s reload

查看端口号的占用情况

  • netstat -tlnp

配置反向代理的一些指令

server{
	listen 80;
    server_name localhost;
    location / {
    	proxy_pass https://www.baidu.com;
    }
}

proxy_pass: 要代理的服务器地址
proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。

proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。

proxy_read_timeout : 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。

proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。

proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh。

配置PC与移动端适配

server{
	listen 80;
    server_name localhost;
    location / {
    	root /usr/share/nginx/pc;
        if ($http_user_agent ~* '(Andriod|iPhone|webOS)') {
           root /usr/share/nginx/mobile;
        }
        index index.html
    }
}

mkdir xxx 创建文件夹
vim xxx 创建文件/进入文件

文章来源,更多的nginx搭建请打开传送门一个前端必会的 Nginx免费教程