在我开始之前我应该注意到绝对没有我具有 nginx 或网络方面的经验,因此我所做的一切都归功于搜索和尝试不同的解决方案。
我遇到了一个问题,已经困扰我一个小时了。
我正在尝试在 nginx 上设置 cgit,以便可以通过子目录访问它,例如/git
。问题是,即使我设法加载界面,css 和图像也会返回 404。
当我访问 时192.168.0.12/git
,我得到的是:
200 /git/ 192.168.0.12/git/
404 cgit.css 192.168.0.12/git/cgit.css
404 cgit.png 192.168.0.12/git/cgit.png
404 favicon.ico 192.168.0.12/git/cgit.png
这是我的当前nginx.conf
:
server {
listen 80;
server_name localhost;
root /var/www/html;
location /git {
root /var/www/cgit;
include fastcgi_params;
fastcgi_split_path_info ^(/git/?)(.+)$;
fastcgi_param SCRIPT_FILENAME $document_root/cgit.cgi;
fastcgi_param QUERY_STRING $args;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_HOST $server_name;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
location ~* \.(css|png|ico) {
expires 24h;
}
}
location / {
try_files $uri/index.html =404;
}
}
和cgitrc
:
css=/git/cgit.css
logo=/git/cgit.css
favicon=/git/favicon.ico
cache-root=/var/cache/cgit
remove-suffix=1
virtual-root=/git/
scan-paht=/srv/git/
我的猜测是我需要以某种方式告诉 nginx 它可以在哪里找到这些文件,但我不知道如何去做,甚至不知道从哪里开始寻找解决方案。
答案1
所有这些路径在您的系统上可能有所不同(我在 ubuntu 上),但总体思路是相同的!
你可以通过将其添加到你的配置中来实现
location /cgit-css/ {
alias /usr/share/cgit/;
expires 24h;
try_files $uri =404;
}
/usr/share/cgit
这将把包含 css 和其他文件(在 ubuntu 上!)的本地文件夹映射到cgit-css
,然后从配置文件中调用:
css=/cgit-css/cgit.css
logo=/cgit-css/cgit.png
favicon=/cgit-css/favicon.ico
...
这是我的完整 nginx 配置:
server {
listen 5555;
server_name _;
root /usr/lib/cgit;
location / {
try_files $uri @cgit;
}
location /cgit-css/ {
alias /usr/share/cgit/;
expires 24h;
try_files $uri =404;
}
location @cgit {
index cgit.cgi;
fastcgi_param SCRIPT_FILENAME $document_root/cgit.cgi;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param HTTP_HOST $server_name;
fastcgi_param PATH_INFO $uri;
fastcgi_param QUERY_INFO $args;
include "fastcgi_params";
}
}
并完成cgitrc
# cgit config
# see cgitrc(5) for details
#
css=/cgit-css/cgit.css
logo=/cgit-css/cgit.png
favicon=/cgit-css/favicon.ico
# Highlight source code with python pygments-based highlighter
source-filter=/usr/lib/cgit/filters/syntax-highlighting.sh
# Format markdown, restructuredtext, manpages, text files, and html files
# through the right converters
about-filter=/usr/lib/cgit/filters/about-formatting.sh
## Search for these files in the root of the default branch of repositories
## for coming up with the about page:
readme=:README.md
readme=:readme.md
readme=:README.mkd
readme=:readme.mkd
readme=:README.rst
readme=:readme.rst
readme=:README.html
readme=:readme.html
readme=:README.htm
readme=:readme.htm
readme=:README.txt
readme=:readme.txt
readme=:README
readme=:readme
readme=:INSTALL.md
readme=:install.md
readme=:INSTALL.mkd
readme=:install.mkd
readme=:INSTALL.rst
readme=:install.rst
readme=:INSTALL.html
readme=:install.html
readme=:INSTALL.htm
readme=:install.htm
readme=:INSTALL.txt
readme=:install.txt
readme=:INSTALL
readme=:install
## List of common mimetypes
mimetype.gif=image/gif
mimetype.html=text/html
mimetype.jpg=image/jpeg
mimetype.jpeg=image/jpeg
mimetype.pdf=application/pdf
mimetype.png=image/png
mimetype.svg=image/svg+xml
mimetype-file=/etc/mime.types
root-title=my git
root-desc=Git Repositories
virtual-root=/
enable-commit-graph=1
enable-index-links=1
enable-log-linecount=1
enable-http-clone=1
enable-index-owner=0
repository-sort=age
case-sensitive-sort=0
# Enable caching of up to 1000 output entries
cache-size=1000
agefile=info/web/last-modified
section-from-path=1
scan-path=/mnt/data/git