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 配置实现对本地静态文件的访问 要使 Nginx 能够访问并提供本地静态文件,需完成以下几个方面的配置: #### 1. 安装 Nginx 首先需要安装 Nginx。可以通过官方提供的下载链接获取最新版本的二进制包[^1]。 ```bash wget http://nginx.org/download/nginx-<version>.tar.gz tar -zxvf nginx-<version>.tar.gz cd nginx-<version> ./configure && make && sudo make install ``` 或者直接从 Windows 或 Linux 的软件仓库中安装。 --- #### 2. 修改 `nginx.conf` 文件 编辑 Nginx配置文件 `nginx.conf`,通常位于 `/etc/nginx/nginx.conf` 或者安装路径下的 `conf/nginx.conf` 中。以下是针对本地静态资源的一个典型配置示例[^2]: ```nginx server { listen 80; server_name www.example-local.com; # 访问根目录设置 root D:/Java/static_files; # 自动索引功能开启 (可选) autoindex on; # 对特定 URL 映射到指定本地目录 location /file/ { alias D:/Java/file/; } # 提供其他类型的静态资源支持 location /images/ { alias D:/Java/images/; } } ``` 上述配置说明: - **root**: 设置默认文档根目录。 - **alias**: 将某个 URI 地址映射至具体的磁盘位置。 - **autoindex**: 开启自动索引功能以便浏览目录结构[^3]。 --- #### 3. 启动或重启 Nginx 服务 保存修改后的配置文件后,重新加载 Nginx 配置使其生效: ```bash sudo systemctl restart nginx # 如果使用 systemd 管理的服务 # 或者对于手动运行的情况 D:\nignx\gerenxiangmu\niginx-1.12.2> nginx -s reload ``` --- #### 4. 测试配置有效性 打开浏览器输入测试地址验证是否可以正常访问本地静态文件。例如: - 图片:`http://www.example-local.com/images/test.jpg` - 文档:`http://www.example-local.com/file/document.pdf` 如果一切配置无误,则能够看到对应的静态资源被正确返回[^4]。 --- ### 注意事项 - 确保所指代的本地路径存在且具有读取权限。 - 若遇到跨域问题,在必要时调整 CORS 相关头信息。 - 使用 HTTPS 加密传输敏感数据更加安全可靠。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值