nginx 配置 本地启动

1.nginx下载地址:nginx: download

    nginx详解:Nginx配置终极版指南(全网最详细)_nginx_脚本之家

2.vue 项目打包生成dist文件里面的文件复制到下载好的nginx的html目录下

3.配置nginx配置文件

打包生成的dist前端包都是属于生产环境的,打包前服务加了/api/后缀 ,axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? '/api' : window.location.protocol+'//'+window.location.host + '/api';


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /api/ {
            proxy_pass http://10.7.37.66:8443/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

4.启动nginx

5.访问localhost:80 进行登录项目,登录成功说明本地配置代理成功

### 配置 Nginx 启动 Vue 应用程序 #### 创建 Vue 应用并构建生产版本 为了准备部署,先创建一个名为 `vue-nginx-docker` 的 Vue 应用: ```bash npx @vue/cli create vue-nginx-docker ``` 进入项目目录并执行构建命令来生成用于生产的静态资源文件[^1]。 ```bash cd vue-nginx-docker npm run build ``` 这会在项目的根目录下生成一个 `dist/` 文件夹,其中包含了打包后的 HTML 和 JavaScript 资源文件。 #### 安装和验证 Nginx 是否正常工作 确认 Linux 系统中已正确安装 Nginx 可通过以下命令完成: ```bash whereis nginx ``` 如果返回路径信息,则表示已经成功安装;反之则需按照官方文档指引进行安装操作[^2]。 #### 修改 Nginx 配置支持前端路由模式 对于单页面应用 (SPA),当刷新浏览器或直接访问带有参数的 URL 时,默认情况下会触发 404 错误。为了避免这种情况发生,在 `/etc/nginx/sites-available/default` 或自定义站点配置文件内添加如下设置以确保所有请求都被重定向到 index.html : ```nginx server { listen 80; server_name localhost; location / { root /path/to/dist; # 替换成实际 dist 所在位置 try_files $uri $uri/ /index.html; } error_page 500 502 503 504 /50x.html; } ``` 上述配置中的 `try_files` 指令可以处理 SPA 中常见的哈希历史模式问题,并且能够使前后端分离架构下的 API 请求被正确转发给后端服务[^3]。 #### 测试与优化 保存更改之后重启 Nginx 来加载新的配置: ```bash sudo systemctl restart nginx # 或者使用此命令 ./nginx -s reload ``` 此时应该可以在本地计算机上打开 Web 浏览器并通过 IP 地址加端口号的方式查看运行的应用程序了。如果有任何错误提示,请检查日志文件帮助定位原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值