我希望将每个对http://localhost/static/
的请求都定向到/usr/share/nginx/static
。
例如,如果用户请求http://localhost/static/style/style.css
,我希望服务器使用/usr/share/nginx/static/style/style.css
的内容进行回复。
这是我的代码(无效):
server {
server_name http://localhost/;
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
}
这是主要的配置文件:
worker_processes 1;
events {
worker_connections 200;
}
http {
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging Settings
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Include Other Configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
更新:我有一个名为mysite.conf的文件,它不会像我在问题中提到的那样重写URL。
location /static {
root /usr/share/nginx/static;
}
答案 0 :(得分:0)
替换
server_name http://localhost/;
作者
server_name localhost;
在server_name
指令后的每个https://nginx.org/en/docs/http/ngx_http_core_module.html#server_name中,您放置服务器名称,而不是URL。
也rewrite
进行HTTP重定向,所以代替
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
使用:
location /static {
root /usr/share/nginx;
}
有关其他示例,请参见此其他答案:Use nginx to serve static files from subdirectories of a given directory