![通过 nginx(proxy_pass)提供 IIS 网站不显示 CSS、图像或 js(404 未找到)](https://linux22.com/image/668701/%E9%80%9A%E8%BF%87%20nginx%EF%BC%88proxy_pass%EF%BC%89%E6%8F%90%E4%BE%9B%20IIS%20%E7%BD%91%E7%AB%99%E4%B8%8D%E6%98%BE%E7%A4%BA%20CSS%E3%80%81%E5%9B%BE%E5%83%8F%E6%88%96%20js%EF%BC%88404%20%E6%9C%AA%E6%89%BE%E5%88%B0%EF%BC%89.png)
我在 Ubuntu 14.04 LTS 上运行了一个 nginx 反向代理。当向 nginx 代理发出请求时,它会将其传递给包含一个(默认)网站的 IIS 服务器。它似乎可以很好地呈现 HTML,但不幸的是,服务器对所有其他资产都响应了 404 错误。
文件如下/etc/nginx/nginx.conf
:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
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;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这是我的default
nginx 服务器配置:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
proxy_pass http://104.46.52.226/;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
example.com
以下是对或www.example.com
(hosts 文件)的请求的结果有已更改):
我猜想这可能是由于我的服务器配置中没有root
指令造成的,但我真的不知道从哪里开始解决这个问题。至少我知道这不是 IIS 服务器本身的问题,因为我可以完美地浏览本地主机。
有什么想法吗?提前致谢。
编辑:这也是我的错误日志:
2015/10/13 09:41:10 [emerg] 3069#0: invalid number of arguments in "proxy_pass" directive in /etc/nginx/sites-enabled/armari.com:31
2015/10/13 10:12:33 [emerg] 3606#0: "listen" directive is not allowed here in /etc/nginx/sites-enabled/armari.com:2
2015/10/13 13:39:18 [error] 3911#0: *1 open() "/usr/share/nginx/html/css/new-web.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/new-web.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:18 [error] 3911#0: *3 open() "/usr/share/nginx/html/css/nivo/themes/default/default.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/themes/default/default.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:18 [error] 3911#0: *4 open() "/usr/share/nginx/html/css/nivo/nivo-slider.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/nivo-slider.css HTTP/1.1", host: "www.armari.com", referrer: "http://www.armari.com/"
2015/10/13 13:39:21 [error] 3911#0: *8 open() "/usr/share/nginx/html/css/new-web.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/new-web.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"
2015/10/13 13:39:21 [error] 3909#0: *10 open() "/usr/share/nginx/html/css/nivo/themes/default/default.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/themes/default/default.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"
2015/10/13 13:39:21 [error] 3909#0: *11 open() "/usr/share/nginx/html/css/nivo/nivo-slider.css" failed (2: No such file or directory), client: 188.39.188.214, server: armari.com, request: "GET /css/nivo/nivo-slider.css HTTP/1.1", host: "armari.com", referrer: "http://armari.com/"
答案1
最终找到了解决方案 - 我强制 nginx 服务器尝试使用 本地提供文件try_files $uri $uri/ =404;
。我所要做的就是删除此行,它就可以完美运行 :)
location / {
proxy_pass http://104.46.52.226/;
}
答案2
从最不可能的问题开始,80 是正确的端口吗proxy_pass http://104.46.52.226/
?如果不是,它监听的是哪个端口?如果是 5000,则需要 proxy_passhttp://104.46.52.226:5000;这可能不是你的问题。
您需要在位置块中设置一些标题,以便 IIS 可以使用它们:
proxy_set_header x-real-IP $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header host $http_host;
去引用卡尔·塞金:
由于 Nginx 实际上是向我们的 Web 服务器发出请求,因此我们在请求中添加了标头,以便 Web 服务器拥有所有必要的信息。
他还谈到了缓存。
文件/etc/nginx/sites-enabled/armari.com
似乎有一行注释掉了server {
。这是错误之一。所有文件都/etc/nginx/sites-enabled/
需要正常工作,Nginx 才能正常工作。如果是这个文件,那么你可能只是没有粘贴#
(注释开始代码)。或者,也许你在错误发生后更改了它。