不灭的焱

革命尚未成功,同志仍须努力 下载Java21

作者:AlbertWen  添加时间:2026-06-22 13:05:46  修改时间:2026-07-06 05:05:25  分类:04.运维体系  编辑

下面给你一份 子路径为 /ticket,但 osTicket 物理目录不在主站目录下 的 Nginx 配置方案。

也就是这种部署方式:

访问地址:
https://your-domain.com/ticket/

主站目录:
/www/wwwroot/your-domain.com

osTicket 真实目录:
/www/wwwroot/osticket

或者:

主站目录:
/www/wwwroot/aiops.fuyo.com

osTicket 真实目录:
/data/www/osticket

这种情况 不能简单用 root,要用 alias

一、目录结构示例

假设你的主站是:

/www/wwwroot/your-domain.com

osTicket 实际放在:

/www/wwwroot/osticket

osTicket 目录结构大概是:

/www/wwwroot/osticket/
├── index.php
├── api/
├── scp/
├── include/
├── js/
├── css/
├── assets/
└── ...

访问路径是:

https://your-domain.com/ticket/

后台路径是:

https://your-domain.com/ticket/scp/

API 路径是:

https://your-domain.com/ticket/api/

二、完整 Nginx 配置方案

下面是完整 server 配置示例。

你需要把:

your-domain.com
/www/wwwroot/osticket
/tmp/php-cgi-83.sock

改成你的真实环境。

server {
    listen 80;
    server_name your-domain.com;

    root /www/wwwroot/your-domain.com;
    index index.php index.html index.htm;

    access_log /www/wwwlogs/your-domain.access.log;
    error_log  /www/wwwlogs/your-domain.error.log;

    client_max_body_size 50m;

    # /ticket 自动跳转到 /ticket/
    location = /ticket {
        return 301 /ticket/;
    }

    # osTicket 前台入口
    location /ticket/ {
        alias /www/wwwroot/osticket/;
        index index.php index.html index.htm;

        try_files $uri $uri/ /ticket/index.php?$query_string;
    }

    # osTicket 后台入口
    location /ticket/scp/ {
        alias /www/wwwroot/osticket/scp/;
        index index.php index.html index.htm;

        try_files $uri $uri/ /ticket/scp/index.php?$query_string;
    }

    # osTicket API 入口
    location /ticket/api/ {
        alias /www/wwwroot/osticket/api/;

        try_files $uri $uri/ /ticket/api/http.php?$query_string;
    }

    # 重点:支持 ajax.php/content/12/manage 这种 PATH_INFO 地址
    location ~ ^/ticket/(.+?\.php)(/.*)?$ {
        alias /www/wwwroot/osticket/$1;

        fastcgi_pass unix:/tmp/php-cgi-83.sock;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME /www/wwwroot/osticket/$1;
        fastcgi_param SCRIPT_NAME /ticket/$1;
        fastcgi_param PATH_INFO $2;
        fastcgi_param PATH_TRANSLATED /www/wwwroot/osticket$2;
    }

    # 禁止访问 osTicket 配置文件
    location ~* ^/ticket/include/ost-config\.php$ {
        deny all;
    }

    # 安装完成后禁止访问 setup
    location ~* ^/ticket/setup/ {
        deny all;
    }

    # 禁止访问敏感目录
    location ~* ^/ticket/(sql|doc|scripts)/ {
        deny all;
    }

    # 禁止访问隐藏文件
    location ~ /\. {
        deny all;
    }

    # osTicket 静态资源缓存
    location ~* ^/ticket/.+\.(jpg|jpeg|png|gif|ico|css|js|svg|webp|woff|woff2|ttf|eot)$ {
        alias /www/wwwroot/osticket/;
        expires 7d;
        access_log off;
    }
}

三、HTTPS 版本

如果你已经开启 SSL,用这个版本:

server {
    listen 80;
    server_name your-domain.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name your-domain.com;

    root /www/wwwroot/your-domain.com;
    index index.php index.html index.htm;

    ssl_certificate     /www/server/panel/vhost/cert/your-domain.com/fullchain.pem;
    ssl_certificate_key /www/server/panel/vhost/cert/your-domain.com/privkey.pem;

    access_log /www/wwwlogs/your-domain.access.log;
    error_log  /www/wwwlogs/your-domain.error.log;

    client_max_body_size 50m;

    location = /ticket {
        return 301 /ticket/;
    }

    location /ticket/ {
        alias /www/wwwroot/osticket/;
        index index.php index.html index.htm;

        try_files $uri $uri/ /ticket/index.php?$query_string;
    }

    location /ticket/scp/ {
        alias /www/wwwroot/osticket/scp/;
        index index.php index.html index.htm;

        try_files $uri $uri/ /ticket/scp/index.php?$query_string;
    }

    location /ticket/api/ {
        alias /www/wwwroot/osticket/api/;

        try_files $uri $uri/ /ticket/api/http.php?$query_string;
    }

    # 重点:支持 ajax.php/content/12/manage 这种 PATH_INFO 地址
    location ~ ^/ticket/(.+?\.php)(/.*)?$ {
        alias /www/wwwroot/osticket/$1;

        fastcgi_pass unix:/tmp/php-cgi-83.sock;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME /www/wwwroot/osticket/$1;
        fastcgi_param SCRIPT_NAME /ticket/$1;
        fastcgi_param PATH_INFO $2;
        fastcgi_param PATH_TRANSLATED /www/wwwroot/osticket$2;
    }

    location ~* ^/ticket/include/ost-config\.php$ {
        deny all;
    }

    location ~* ^/ticket/setup/ {
        deny all;
    }

    location ~* ^/ticket/(sql|doc|scripts)/ {
        deny all;
    }

    location ~ /\. {
        deny all;
    }

    location ~* ^/ticket/.+\.(jpg|jpeg|png|gif|ico|css|js|svg|webp|woff|woff2|ttf|eot)$ {
        alias /www/wwwroot/osticket/;
        expires 7d;
        access_log off;
    }
}

四、宝塔面板配置方式

1. 打开站点配置

进入宝塔:

网站 -> 找到主站 your-domain.com -> 设置 -> 配置文件

你会看到类似:

server {
    listen 80;
    server_name your-domain.com;
    root /www/wwwroot/your-domain.com;
    index index.php index.html index.htm;

    include enable-php-83.conf;
    include rewrite/your-domain.com.conf;

    # 其他配置...
}

你需要把 /ticket 相关配置加到当前 server {} 里面。

2. 宝塔中推荐插入的配置片段

把下面这段加入主站的 server {} 中:

# ==============================
# osTicket 子路径 /ticket 映射配置
# 访问路径:https://your-domain.com/ticket/
# 物理目录:/www/wwwroot/osticket
# ==============================

client_max_body_size 50m;

location = /ticket {
    return 301 /ticket/;
}

location /ticket/ {
    alias /www/wwwroot/osticket/;
    index index.php index.html index.htm;

    try_files $uri $uri/ /ticket/index.php?$query_string;
}

location /ticket/scp/ {
    alias /www/wwwroot/osticket/scp/;
    index index.php index.html index.htm;

    try_files $uri $uri/ /ticket/scp/index.php?$query_string;
}

location /ticket/api/ {
    alias /www/wwwroot/osticket/api/;

    try_files $uri $uri/ /ticket/api/http.php?$query_string;
}

# 重点:支持 ajax.php/content/12/manage 这种 PATH_INFO 地址
location ~ ^/ticket/(.+?\.php)(/.*)?$ {
    alias /www/wwwroot/osticket/$1;

    fastcgi_pass unix:/tmp/php-cgi-83.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME /www/wwwroot/osticket/$1;
    fastcgi_param SCRIPT_NAME /ticket/$1;
    fastcgi_param PATH_INFO $2;
    fastcgi_param PATH_TRANSLATED /www/wwwroot/osticket$2;
}

location ~* ^/ticket/include/ost-config\.php$ {
    deny all;
}

location ~* ^/ticket/setup/ {
    deny all;
}

location ~* ^/ticket/(sql|doc|scripts)/ {
    deny all;
}

location ~* ^/ticket/.+\.(jpg|jpeg|png|gif|ico|css|js|svg|webp|woff|woff2|ttf|eot)$ {
    alias /www/wwwroot/osticket/;
    expires 7d;
    access_log off;
}

五、宝塔里最重要的注意点

1. /ticket 的 PHP 解析配置要放在通用 PHP 配置前面

宝塔站点配置里通常有这一行:

include enable-php-83.conf;

这个文件里面一般包含通用 PHP 解析规则,例如:

location ~ [^/]\.php(/|$) {
    # ...
}

你的 /ticket PHP 规则:

location ~ ^/ticket/(.+\.php)$ {
    ...
}

最好放在:

include enable-php-83.conf;

的前面。

原因是:Nginx 的正则 location 会按出现顺序匹配。 如果宝塔通用 PHP 规则先匹配了 /ticket/index.php,它会去主站目录下找:

/www/wwwroot/your-domain.com/ticket/index.php

但你的真实文件在:

/www/wwwroot/osticket/index.php

这样就会导致:

404
No input file specified
File not found

推荐结构:

server {
    listen 80;
    server_name your-domain.com;
    root /www/wwwroot/your-domain.com;

    # 先放 /ticket 的 alias 和 PHP 规则
    location = /ticket {
        return 301 /ticket/;
    }

    location /ticket/ {
        alias /www/wwwroot/osticket/;
        try_files $uri $uri/ /ticket/index.php?$query_string;
    }

    # 重点:支持 ajax.php/content/12/manage 这种 PATH_INFO 地址
    location ~ ^/ticket/(.+?\.php)(/.*)?$ {
        alias /www/wwwroot/osticket/$1;

        fastcgi_pass unix:/tmp/php-cgi-83.sock;
        fastcgi_index index.php;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME /www/wwwroot/osticket/$1;
        fastcgi_param SCRIPT_NAME /ticket/$1;
        fastcgi_param PATH_INFO $2;
        fastcgi_param PATH_TRANSLATED /www/wwwroot/osticket$2;
    }

    # 再放宝塔原有 PHP 配置
    include enable-php-83.conf;

    # 其他配置...
}

2. alias 后面必须带 /

这个是重点。

正确:

location /ticket/ {
    alias /www/wwwroot/osticket/;
}

错误:

location /ticket/ {
    alias /www/wwwroot/osticket;
}

少了最后的 /,容易导致静态资源、附件、CSS、JS 路径异常。

3. location /ticket/ 也必须带 /

正确:

location /ticket/ {
    alias /www/wwwroot/osticket/;
}

错误:

location /ticket {
    alias /www/wwwroot/osticket/;
}

建议单独加:

location = /ticket {
    return 301 /ticket/;
}

/ticket 自动跳到 /ticket/

六、PHP Socket 怎么确认?

你需要确认宝塔 PHP 的 socket。

执行:

ls /tmp/php-cgi-*.sock

可能看到:

/tmp/php-cgi-74.sock
/tmp/php-cgi-80.sock
/tmp/php-cgi-81.sock
/tmp/php-cgi-82.sock

如果你使用 PHP 8.1:

fastcgi_pass unix:/tmp/php-cgi-81.sock;

如果你使用 PHP 8.2:

fastcgi_pass unix:/tmp/php-cgi-82.sock;

如果你使用 PHP 8.3:

fastcgi_pass unix:/tmp/php-cgi-83.sock;

七、目录权限建议

假设 Nginx/PHP 运行用户是 www,执行:

chown -R www:www /www/wwwroot/osticket
find /www/wwwroot/osticket -type d -exec chmod 755 {} \;
find /www/wwwroot/osticket -type f -exec chmod 644 {} \;
chmod 640 /www/wwwroot/osticket/include/ost-config.php

如果附件目录、上传目录报权限错误,再检查 osTicket 后台的附件存储配置。

八、安装完成后的安全处理

osTicket 安装完成后,建议删除安装目录:

rm -rf /www/wwwroot/osticket/setup

即使没有删除,Nginx 里也已经禁止访问:

location ~* ^/ticket/setup/ {
    deny all;
}

配置文件也禁止外部访问:

location ~* ^/ticket/include/ost-config\.php$ {
    deny all;
}

九、可选:如果主站统一设置了 CSP

如果你的主站配置里有类似:

add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval';";

并且 osTicket 在 /ticket/ 下出现 JS、Worker、blob、data 相关报错,可以给 /ticket/ 单独放宽策略。

示例:

location /ticket/ {
    alias /www/wwwroot/osticket/;
    index index.php index.html index.htm;

    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: data:; worker-src 'self' blob: data:; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self';" always;

    try_files $uri $uri/ /ticket/index.php?$query_string;
}

不过这个不是必须项。只有你确认 /ticket/ 受到主站 CSP 影响时再加。

十、检查配置

修改完成后,先测试 Nginx 配置:

nginx -t

如果显示:

syntax is ok
test is successful

再重载:

systemctl reload nginx

宝塔里也可以:

软件商店 -> Nginx -> 重载配置

十一、访问测试

前台:

https://your-domain.com/ticket/

后台:

https://your-domain.com/ticket/scp/

API:

https://your-domain.com/ticket/api/

测试静态资源是否正常:

https://your-domain.com/ticket/css/
https://your-domain.com/ticket/js/

实际是否能列目录不重要,关键是浏览器开发者工具里 CSS、JS 不要 404。

十二、最终推荐你在宝塔使用的核心配置

如果你的真实目录是:

/www/wwwroot/osticket

你的访问路径是:

/ticket

那么直接用这段:

# ==============================
# osTicket 子路径 /ticket 映射配置
# 访问路径:https://your-domain.com/ticket/
# 物理目录:/www/wwwroot/osticket
# ==============================

client_max_body_size 50m;

# /ticket 自动跳转到 /ticket/
location = /ticket {
    return 301 /ticket/;
}

# osTicket 前台入口
location /ticket/ {
    alias /www/wwwroot/osticket/;
    index index.php index.html index.htm;

    try_files $uri $uri/ /ticket/index.php?$query_string;
}

# osTicket 后台入口
location /ticket/scp/ {
    alias /www/wwwroot/osticket/scp/;
    index index.php index.html index.htm;

    try_files $uri $uri/ /ticket/scp/index.php?$query_string;
}

# osTicket API 入口
location /ticket/api/ {
    alias /www/wwwroot/osticket/api/;

    try_files $uri $uri/ /ticket/api/http.php?$query_string;
}
​​​​​​​
# 重点:解析 /ticket 下的 PHP 文件
# 同时支持 ajax.php/content/12/manage 这种 PATH_INFO 地址
location ~ ^/ticket/(.+?\.php)(/.*)?$ {
    alias /www/wwwroot/osticket/$1;

    fastcgi_pass unix:/tmp/php-cgi-83.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME /www/wwwroot/osticket/$1;
    fastcgi_param SCRIPT_NAME /ticket/$1;
    fastcgi_param PATH_INFO $2;
    fastcgi_param PATH_TRANSLATED /www/wwwroot/osticket$2;
}

# 禁止访问 osTicket 配置文件
location ~* ^/ticket/include/ost-config\.php$ {
    deny all;
}

# 安装完成后禁止访问 setup
location ~* ^/ticket/setup/ {
    deny all;
}

# 禁止访问敏感目录
location ~* ^/ticket/(sql|doc|scripts)/ {
    deny all;
}

# osTicket 静态资源缓存
location ~* ^/ticket/.+\.(jpg|jpeg|png|gif|ico|css|js|svg|webp|woff|woff2|ttf|eot)$ {
    alias /www/wwwroot/osticket/;
    expires 7d;
    access_log off;
}

放置位置建议:

server {
    ...

    # 这里放 /ticket 配置
    # 必须放在 include enable-php-xx.conf 前面

    include enable-php-83.conf;

    ...
}

这样 /ticket 是访问路径,/www/wwwroot/osticket 是真实物理目录,二者不需要在同一个主站目录下。