当前位置: 首页 > article >正文

nginx安装并部署前端项目【包括Linux与Windows系统】

nginx安装并部署前端项目

  • 一、 nginx下载与安装
  • 二、 前端项目部署
  • 三、 常用命令&注意事项
  • 四、 常见问题【持续更新】

一、 nginx下载与安装

① 下载地址:https://nginx.org/en/download.html

② 下载教程:根据不同操作系统(Linux或者Windows)下载稳定版本(Stable version)到本地

③ 启动nginx:Windows系统双击nginx.exe,若双击未弹出内容,则说明可能端口被占用【或者使用命令行cmd:输入start nginx】;Linux系统使用命令./nginx 启动

二、 前端项目部署

一般主要配置http下sever字段即可部署成功,当然实际使用还需根据大家特殊情况进行特殊配置,示例配置及说明如下:

http {
    #根据需要自行添加配置
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    server {
        listen       8009;  #页面访问端口,若被占用可更换,修改完重启nginx即可
        server_name  localhost;
        
		#浏览器访问前端页面的地址
		location /test/chatPage {
			alias html/chatPage;  #前端页面代码路径
			index index.html index.htm;
			#如果请求的资源在静态文件目录中找不到,则重定向到index.html
			try_files $uri $uri/ /index.html;
		}
		
		#代理页面需要调用的接口,若不是本地接口,则可通过代理到其他环境接口获取数据进行调试		
		location /test/chat {
            proxy_pass http://xx.xxx.xxx.xx:8009/test/chat;
        }
		
        #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;
    #    }
    #}

}

三、 常用命令&注意事项

  1. tasklist | findstr nginx(就能查到开启的nginx)

  2. taskkill /F /IM nginx.exe(能强制删除所有开的nginx),注意:开nginx的时候不要猛点

  3. Linux系统:

    cd /usr/local/nginx/sbin/

    ./nginx 启动

    ./nginx -s stop 停止

    ./nginx -s quit 安全退出

    ./nginx -s reload 重新加载配置文件

    ps aux|grep nginx 查看nginx进程

  4. windows系统:

    nginx.exe 启动

    nginx -s stop 停止

    nginx -s quit 安全退出

    nginx -s reload 重新加载配置文件

    ps aux|grep nginx 查看nginx进程

四、 常见问题【持续更新】

  1. pc端用iframe嵌入智能体,页面无法加载,报Refused to display ‘’ in a frame because it set ‘X-Frame-Options’ to ‘sameorigin’.
server {
    listen 80;
    server_name yourdomain.com;

    location /proxy/ {
        proxy_pass https://targetdomain.com/;
        proxy_set_header Host targetdomain.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # 删除X-Frame-Options头部
        proxy_hide_header X-Frame-Options;

        # 可选:设置Content-Security-Policy来允许来自特定源的frame嵌入
        add_header Content-Security-Policy "frame-ancestors 'self' yourdomain.com";
    }
}

欢迎大家批评指正,接纳更好的建议哦!会持续更新~~


http://www.kler.cn/a/542882.html

相关文章:

  • Unity URP后处理在Game窗口不显示
  • 基于 SpringBoot 和 Vue 的智能腰带健康监测数据可视化平台开发(文末联系,整套资料提供)
  • JVM 类加载子系统在干什么?
  • 具身智能训练新思路!将生成视频用于训练机器人
  • 基于Python的人工智能驱动基因组变异算法:设计与应用(下)
  • 推荐一款 免费的SSL,自动续期
  • 前端性能分析常见内容
  • C语言蓝桥杯1003: [编程入门]密码破译
  • 香港中文大学 Adobe 推出 MotionCanvas:开启用户掌控的电影级图像视频创意之旅。
  • 基于Springboot(Maven项目)——分页
  • 在 debian 12 上安装 mysqlclient 报错
  • 代码随想录--977有序数组的平方
  • Java Stream 全面解析
  • 使用EVE-NG-锐捷实现三层数据通信
  • 曝苹果2026年秋季推首款折叠iPhone
  • Git 安全与权限管理
  • 深入探索现代CSS:从基础到未来趋势
  • 128,【1】buuctf [极客大挑战 2019]PHP
  • 浅谈自己对RPC的理解
  • AspectJ 下 Advisor 的排序过程
  • untiy3d 触发和碰撞区别
  • innovus如何分步长func和dft时钟
  • MacBook Pro M2安装deepseek
  • C++20 新特性解析
  • 科技查新要怎么做
  • 链表分割-双哨兵位