您的位置 首页 nginx

nginx web 放到 location /app1 /app2 放多个网站

 

1准备文件:

mkdir -p /app/code/web1/{static,images}
mkdir -p /app/code/web2/{static,images}

创建基本的 HTML 文件

    cat <<EOF > /app/code/web1/index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Web1 Home</title>
        <link rel="stylesheet" href="/app1/static/style.css">
    </head>
    <body>
        <h1>Welcome to Web1</h1>
        <p>This is the homepage for Web1.</p>
        <img src="/app1/images/test.jpg" alt="Test Image">
    </body>
    </html>
EOF
    

web2:

    cat <<EOF > /app/code/web2/index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Web2 Home</title>
        <link rel="stylesheet" href="/app2/static/style.css">
    </head>
    <body>
        <h1>Welcome to Web2</h1>
        <p>This is the homepage for Web2.</p>
        <img src="/app2/images/test.jpg" alt="Test Image">
    </body>
    </html>
EOF
    

创建静态资源文件

web1/static/style.css:

    cat <<EOF > /app/code/web1/static/style.css
    body {
        font-family: Arial, sans-serif;
        background-color: #f0f0f0;
        margin: 0;
        padding: 20px;
    }

    h1 {
        color: #333;
    }

    p {
        color: #666;
    }
EOF
    

web2/static/style.css:

    cat <<EOF > /app/code/web2/static/style.css
    body {
        font-family: Arial, sans-serif;
        background-color: #e0e0e0;
        margin: 0;
        padding: 20px;
    }

    h1 {
        color: #444;
    }

    p {
        color: #777;
    }
EOF
    

创建图片文件

web1/images/test.jpg:

wget -O /app/code/web1/images/test.jpg https://www.baimeidashu.com/wp-content/uploads/2023/10/Pasted-10.png

web2

 wget -O /app/code/web2/images/test.jpg https://www.baimeidashu.com/wp-content/uploads/2024/10/Pasted-44.png

3. 确认 Nginx 用户权限

chown -R nginx:nginx /app/code/
chmod -R 755 /app/code/

配置文件 /etc/nginx/conf.d/1shop1.kattgatt.com.conf

server {
    listen 8086;
    server_name shop.kattgat.com;

    # 配置第一个网站 /app1
    location /app1/ {
        alias /app/code/web1/;
        index index.html;
        try_files $uri $uri/ /app1/index.html;
    }

    # 配置第二个网站 /app2
    location /app2/ {
        alias /app/code/web2/;
        index index.html;
        try_files $uri $uri/ /app2/index.html;
    }

    # 配置静态资源路径 /app1/static/
    location /app1/static/ {
        alias /app/code/web1/static/;
        expires 30d;  # 可选:添加缓存控制
    }

    # 配置静态资源路径 /app2/static/
    location /app2/static/ {
        alias /app/code/web2/static/;
        expires 30d;  # 可选:添加缓存控制
    }

    # 处理图片等资源 /app1/images/
    location /app1/images/ {
        alias /app/code/web1/images/;
        expires 30d;  # 可选:添加缓存控制
    }

    # 处理图片等资源 /app2/images/
    location /app2/images/ {
        alias /app/code/web2/images/;
        expires 30d;  # 可选:添加缓存控制
    }
}

 

访问:

http://10.0.0.204:8086/app1/

http://10.0.0.204:8086/app2/

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章