• Nginx部署Vue前端项目


    目录

    一、安装Nginx

    1、安装完成

    2、启动Nginx

    3、问题

    二、修改Nginx配置文件


    系统环境:Mac Pro—10.15.7版本

    Nginx版本:1.19.6

    一、安装Nginx

    brew install nginx

    1、安装完成

    Nginx的配置文件目录:/usr/local/etc/nginx

    Nginx的安装目录:/usr/local/Cellar/nginx

    2、启动Nginx

    brew services start nginx

    3、问题

    可能遇到的报错一:nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

    解决:原因是默认nginx主配置文件位置与你当前不一样。使用终端进入到Nginx安装位置,然后输入 【./nginx -h】 即可查看

     找到-e filename那一栏,将default后面的路径复制下来,然后使用命令指定nginx.conf文件的位置:

    【./nginx -c /usr/local/etc/nginx/nginx.conf】,之后输入命令重新加载:【./nginx -s reload】即可。

    二、修改Nginx配置文件

    打开Nginx配置文件:/usr/local/etc/nginx/nginx.conf

    需要修改3处,下面的配置文件中已添加注释,查看“注意”开头的说明

    1. root后面的前端打包好的文件路径;
    2. location后面的映射路径,该路径为服务端Swagger配置的pathMapping值;
    3. proxy_pass后面的本机ip地址;
    1. #user nobody;
    2. worker_processes 1;
    3. #pid logs/nginx.pid;
    4. pid /usr/local/Cellar/nginx/1.19.6/logs/nginx.pid;
    5. events {
    6. worker_connections 1024;
    7. }
    8. http {
    9. include mime.types;
    10. default_type application/octet-stream;
    11. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    12. # '$status $body_bytes_sent "$http_referer" '
    13. # '"$http_user_agent" "$http_x_forwarded_for"';
    14. #access_log logs/access.log main;
    15. sendfile on;
    16. #tcp_nopush on;
    17. #keepalive_timeout 0;
    18. keepalive_timeout 65;
    19. gzip on;
    20. server {
    21. listen 80;
    22. server_name localhost;
    23. charset utf-8;
    24. #access_log logs/host.access.log main;
    25. #注意:修改root后面的地址为你打包好的dist文件夹前端项目路径
    26. location / {
    27. root xx/yy/zz/dist;
    28. try_files $uri $uri/ /index.html;
    29. index index.html index.htm;
    30. }
    31. #注意:/prod-api/ 为你的服务端Swagger中配置的pathMapping属性值
    32. #注意:修改proxy_pass后面的地址为你部署机器的ip地址,切记端口号后面斜杠不要去掉【/】
    33. location /prod-api/ {
    34. proxy_set_header Host $http_host;
    35. proxy_set_header X-Real-IP $remote_addr;
    36. proxy_set_header REMOTE-HOST $remote_addr;
    37. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    38. proxy_pass http://0.0.0.0:8081/;
    39. }
    40. #error_page 404 /404.html;
    41. # redirect server error pages to the static page /50x.html
    42. #
    43. error_page 500 502 503 504 /50x.html;
    44. location = /50x.html {
    45. root html;
    46. }
    47. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    48. #
    49. #location ~ \.php$ {
    50. # proxy_pass http://127.0.0.1;
    51. #}
    52. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    53. #
    54. #location ~ \.php$ {
    55. # root html;
    56. # fastcgi_pass 127.0.0.1:9000;
    57. # fastcgi_index index.php;
    58. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    59. # include fastcgi_params;
    60. #}
    61. #省略.......
    62. }
    63. include servers/*;
    64. }

    配置好后输入命令重新加载【./nginx -s reload】。

    之后在浏览器输入本机ip地址即可访问,注意:如果服务端的端口号为80开头,ip地址后面不加端口号

  • 相关阅读:
    java源码系列:HashMap源码验证,自己手写一个HashMap之01-基本框架
    软件设计原则
    k8s如何优雅地关闭Pod
    Spring——简介和IOC底层原理
    本地搭建Stackedit Markdown编辑器结合内网穿透实现远程访问
    InnoDB之redo log写入和恢复
    JS中的防抖和节流
    Java - HashMap原理分析
    PostgreSQL文本搜索(七)——自定义配置
    PMP备考大全:经典题库(敏捷管理第8期)
  • 原文地址:https://blog.csdn.net/u010263943/article/details/128136131