Nginx前端后端共用一个域名如何配置

发布于:2025-02-10 ⋅ 阅读:(95) ⋅ 点赞:(0)

在 Nginx 中配置前端和后端共用一个域名的情况,通常是通过路径或子路径将请求转发到不同的服务。以下是一个示例配置,假设:

前端静态文件在 /var/www/frontend/。
后端 API 服务运行在 http://127.0.0.1:5000。
域名是 example.com,其中:
静态前端通过 example.com 访问。
后端 API 通过 example.com/api/ 访问。

server {
    listen 80;
    server_name example.com;

    # 配置前端静态文件
    location / {
        root /var/www/frontend;
        index index.html;

        # 支持前端 history 模式路由
        try_files $uri $uri/ /index.html;
    }

    # 配置后端 API 路由
    location /api/ {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        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;
    }

    # 配置错误页面(可选)
    error_page 404 /404.html;
    location = /404.html {
        root /var/www/frontend;
    }
}

在前端使用基于路由的单页应用程序(如 React、Vue )时,前端的路由模式通常分为 hash 模式 和 history 模式。在 Nginx 配置前端路由时,需要特别处理 history 模式,因为它依赖于 HTML5 的 pushState 功能,而不带 # 的路径直接被 Nginx 视为文件路径。

try_files 指令的作用就是按顺序检査文件是否存在,返回第一个找到的文件。 $uri 是nginx 提供的变量,指当前请求的 URI,不包括任何参数,当请求静态资源文件的时候,命中 $uri 规则;当请求页面路由的时候,命中 /ndex.html 规则

nginx.conf 配置文件位置及其默认配置

# Intel Chip 系统路径
/usr/local/etc/nginx/nginx.conf

# Apple Silicon 系统路径
/opt/homebrew/etc/nginx/nginx.conf

默认配置

#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;
        }

        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;
    #    }
    #}
    include servers/*;
}


网站公告

今日签到

点亮在社区的每一天
去签到