25,985
社区成员
发帖
与我相关
我的任务
分享
#服务器集群
upstream tomcat{
server localhost:8081;
server localhost:8080;
}
server {
listen 8099;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location ~ \.(jsp)?$ {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 100;
proxy_read_timeout 100;
proxy_send_timeout 100;
}
#配置Nginx的动静分离,使用后缀方式来匹配
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
expires 30d;
root /nginx-1.9.9;
}
server {
listen 8099;
server_name 127.0.0.1;
location ~ .*\.(gif|jpg|jpeg|png)$
{
root /test;
}
其中test是在nginx的根目录下文件夹
访问,test/60.jgp提示404的错误
nginx访问时正常的[/quote]
图片,可以加载出来了,错误原因是location中的root路径写错了
写错绝对路径,就没有问题了,如: root E:/JAVA/nginx-1.9.9/test;
但是,为什么您的写法是相对路径,也没有问题?[/quote]
你是window系统,他是linux系统,/test在linux下就是绝对路径
server {
listen 8099;
server_name 127.0.0.1;
location ~ .*\.(gif|jpg|jpeg|png)$
{
root /test;
}
其中test是在nginx的根目录下文件夹
访问,test/60.jgp提示404的错误
nginx访问时正常的[/quote]
图片,可以加载出来了,错误原因是location中的root路径写错了
写错绝对路径,就没有问题了,如: root E:/JAVA/nginx-1.9.9/test;
但是,为什么您的写法是相对路径,也没有问题?
server {
listen 8099;
server_name 127.0.0.1;
location ~ .*\.(gif|jpg|jpeg|png)$
{
root /test;
}
其中test是在nginx的根目录下文件夹
访问,test/60.jgp提示404的错误
nginx访问时正常的
server
{
listen 8099;
server_name 192.168.1.134;
root /home/wwwroot/default;
location ~ .*\.(gif|jpg|jpeg|png)$
{
root /home/wwwroot/default/test;
}
}
监听的 8099 ,若是图片,则去 /home/wwwroot/default/test 这个目录下去找(test目录下有张 t.jpg 的图片),访问地址是 http://192.168.1.134:8099/t.jpg
测试可以访问,同理,设置其他的location 也是可行的
还有一种方式,设置多个 server ,同时监听 80 端口,server_name 不一样,比如静态的就叫 static.youdomain.com ,这里的root 就设置为 root /nginx-1.9.9; 这样访问 http://static.youdomain.com/test.js,就会跑到 /nginx-1.9.9 目录下去找 test.js
<script type="text/javascript" src="../scripts/boot.js">
重新载入页面以获取源代码: http://localhost:8099/scripts/boot.js
加了,localhost的端口变成了8099,
但是样式还是无法加载出来,访问这个地址http://localhost:8099/scripts/boot.js
提示404错误,那说明动静分离还是没有起效