美文网首页Server服务器
mac安装Nginx及使用

mac安装Nginx及使用

作者: Jesscia_Liu | 来源:发表于2023-06-13 12:01 被阅读0次

一、homebrew安装Nginx

1、安装nginx,查看nginx安装和配置路径

  • 安装
brew install nginx
  • 使用brew安装完成后nginx的安装路径为
 /usr/local/Cellar/nginx
  • 配置文件路径为
/usr/local/etc/nginx/nginx.conf

2、查看nginx信息

brew info nginx

3、使用homebrew方式启动和关闭nginx

  • 启动
brew services start nginx
  • 重启
brew services restart nginx
  • 停止
brew services stop nginx

4、直接使用nginx启动

  • 启动
sudo nginx
  • 关闭
    • stop表示立即停止nginx,不保存相关信息
    • quit表示正常退出nginx,并保存相关信息
sudo nginx -s stop    或者    nginx -s quit
  • 重新加载配置文件
sudo nginx -s reload

5、其他

  • 查看nginx是否启动,存在nginx:master即是启动中的nginx进程号
ps -ef|grep nginx
  • 这将使用默认配置文件启动Nginx服务器。你可以使用以下命令测试Nginx服务器是否正在运行:
curl http://localhost:80

6、安装并启动成功

  • 默认安装后的端口号是80,可在/usr/local/etc/nginx/nginx.conf文件中查看
浏览器中访问:http://localhost:80
  • 启动成功


    启动成功.jpg

二、使用nginx配置启动vue项目

1、打包vue项目

npm run build
把打包好的dist文件夹拷贝到nginx里的html文件夹下


/usr/local/Cellar/nginx/html/dist.jpg

2、修改nginx.conf配置文件

  • 路径: /usr/local/etc/nginx/nginx.conf

1)一个最简单的示例--无反向代理

  • 只修改server中的listen和location,其他使用默认配置
  • 修改后可以直接使用http://localhost:8887 访问vue项目
    server {
        listen       8887; #此处可修改访问的端口号
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            root /usr/local/Cellar/nginx/1.23.1/html/dist/; #此处必须替换为nginx下打包得到dits文件夹路径
            index  index.html index.htm;
        }
    }

2)局域网设备对vue项目的访问

  • 终端输入,查看ip地址
ifconfig | grep "inet"
ip地址.jpg
  • 直接使用 192.168.xx.xxx:8030 可访问

三、参考文章

如何在Mac上启动NGINX

相关文章

网友评论

    本文标题:mac安装Nginx及使用

    本文链接:https://www.haomeiwen.com/subject/mqjaedtx.html