首页 新闻 赞助 找找看

.netcore发布在Centos7上,wwwroot文件404

0
悬赏园豆:60 [已解决问题] 解决于 2018-06-15 10:43

执行站点程序之后,访问一个静态文件提示404

在window下部署是可以正常访问的。

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://192.168.0.64:5002/css/site.css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 0.1579ms 404

 

 

  if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
母鸡轰的主页 母鸡轰 | 初学一级 | 园豆:74
提问于:2018-06-06 15:20
< >
分享
最佳答案
0

确认centos上站点目录中有对应的文件吗?

收获园豆:50
dudu | 高人七级 |园豆:31075 | 2018-06-06 15:31

有的

母鸡轰 | 园豆:74 (初学一级) | 2018-06-06 15:33

@母鸡轰: 你是怎么发布到centos上的?

dudu | 园豆:31075 (高人七级) | 2018-06-06 15:41

@dudu: windows直接 pscp到centos系统上,查看了文件也没有丢失

母鸡轰 | 园豆:74 (初学一级) | 2018-06-06 15:54

@母鸡轰: 建议试试直接在centos上发布

dudu | 园豆:31075 (高人七级) | 2018-06-06 16:00

@dudu: 还是不行

母鸡轰 | 园豆:74 (初学一级) | 2018-06-06 17:00

@母鸡轰: 把 .UseContentRoot(Directory.GetCurrentDirectory()) 放到 .UseStartup<Startup>() 之前试试

dudu | 园豆:31075 (高人七级) | 2018-06-06 17:18

@dudu: 还是不行

母鸡轰 | 园豆:74 (初学一级) | 2018-06-06 17:45

@dudu: 这个是我代码的源码https://files.cnblogs.com/files/jhy55/Lottery.Img.zip

母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 16:28

@母鸡轰: 检测到有病毒,建议放到github

dudu | 园豆:31075 (高人七级) | 2018-06-14 17:39

@dudu: 应该那些dll引起的

母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 17:41

@dudu: https://github.com/vsofojhy/Lottery.Img, 放到hithub了

母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 17:56

@母鸡轰: 静态文件被你放到了 wwwroot\common 文件夹中,应该放在 wwwroot

dudu | 园豆:31075 (高人七级) | 2018-06-14 18:49

@母鸡轰: ValidateUtils.js 是木马文件

dudu | 园豆:31075 (高人七级) | 2018-06-14 18:49

@dudu: 里面只是一些正则的东西,怎么回事木马?请指教

母鸡轰 | 园豆:74 (初学一级) | 2018-06-15 09:11

@dudu: 在wwwroot在不能存在子目录的?如果需要的话需要什么操作

母鸡轰 | 园豆:74 (初学一级) | 2018-06-15 09:24

@母鸡轰: 在 Program 中 添加 .UseWebRoot("wwwroot/common")

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseWebRoot("wwwroot/common")
        .UseStartup<Startup>();
dudu | 园豆:31075 (高人七级) | 2018-06-15 10:26

@dudu: 谢谢,已经解决,静态文件中包含了中文路径,centos的编码跟windows的编码不同导致,在centos启动报错 路径非法

母鸡轰 | 园豆:74 (初学一级) | 2018-06-15 10:42
其他回答(3)
0

页面中怎么引用这些文件的,截个图看一下

MrNice | 园豆:3450 (老鸟四级) | 2018-06-06 16:14

 public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
             .UseKestrel()
             .UseStartup<Startup>()
             .UseUrls("http://*:5002")
             .UseContentRoot(Directory.GetCurrentDirectory())
             .Build();
    }
 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

            services.AddMvc(); 
            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-06-06 17:00
0

你好,两个问题,你的发布配置中:

创造发布站点完毕之后,验证web项目是否可运行,执行命令

dotnet /dotnet/webAPP/Lottery.WEB.dll

我假设你的网站根目录在WebAPP下

看下面你的配置文件中存在如下配置:

command=/bin/bash -c "dotnet Lottery.WEB.dll"

directory=/dotnet/webAPP/admin1.lottery.com/

你在验证时使用 /dotnet/webAPP/Lottery.WEB.dll

而你的路径配置为:/dotnet/webAPP/admin1.lottery.com/

我不确定你的网站根目录设定是正确的,以及你的程序是正确执行的。请你改成如下命令尝试:

command=dotnet /dotnet/webAPP/Lottery.WEB.dll或command=bash /usr/bin/dotnet /dotnet/webAPP/Lottery.WEB.dll

出现404白页主要原因在于:directory=/dotnet/webAPP/此目录配置错误,网站根目录配置错误就会返回404错误。

你的系统经过大佬们检查应该是没有问题的,有问题的就是代理服务器的相关配置问题,可以将你的关键配置给出,方便各位大佬们帮助你快速解决问题。发布过程中排除网络因素,一般不会导致文件丢失,你可以分析404出现的原因分类就容易解决问题了。

本人刚接触网站开发,对开发方面不懂,以上纯属个人观点,希望对你有帮助。

收获园豆:10
河图书卦 | 园豆:206 (菜鸟二级) | 2018-06-14 05:39
[program:Lottery.WEB]
command=bash /usr/bin/dotnet /dotnet/webAPP/admin1.lottery.com/Lottery.WEB.dll
directory=/dotnet/webAPP/admin1.lottery.com/
stderr_logfile=/var/log/Lottery.web.error.log
stdout_logfile=/var/log/Lottery.web.stdout.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=false
startsecs=1

这样子在 wwwroot文件里面没有静态文件的时候可以正常启动,如果wwwroot中存在静态文件启动错误

at System.IO.UnixFileSystem.FileSystemEnumerable`1.<Enumerate>d__11.MoveNext()
   at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
   at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
   at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
   at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
   at System.IO.FileSystemWatcher.RunningInstance.AddDirectoryWatchUnlocked(WatchedDirectory parent, String directoryName)
   at System.IO.FileSystemWatcher.RunningInstance..ctor(FileSystemWatcher watcher, SafeFileHandle inotifyHandle, String directoryPath, Boolean includeSubdirectories, NotifyEvents notifyFilters, CancellationToken cancellationToken)
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b__0_0()
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Lottery.WEB.Program.Main(String[] args) in G:\Project\Lottery\Program\WEB\Lottery.WEB\Program.cs:line 18
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();

        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
            .UseUrls("http://*:5001")
                .Build();
    }
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //验证中间件
            app.UseAuthentication();

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                
            }
            //app.UseExceptionHandler("/Main/Error");
            else
            {
                app.UseExceptionHandler("/Main/Error");
            }

           

            //解决Ubuntu Nginx 代理不能获取IP问题 
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller}/{action}/{id?}"
                );
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Main}/{action=Index}/{id?}");
            });
        }
支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 12:18

@母鸡轰: 第18行是哪句呀?不得不说,对于刚接触网站开发的我来说,看错误日志还是有点难度的。我认为可能是输入法问题导致的,有没有存在中文字符的可能?

还有我之前看见一个很特例的情况,有人发布到centos7后,页面访问出现404问题,解决办法是在linux下,重新将默认路由配置中的default修改为了Default就解决问题了,我的项目中是default并没出现问题,你可以试下把其它配置改为原先的修改这个Default试试。

支持(0) 反对(0) 河图书卦 | 园豆:206 (菜鸟二级) | 2018-06-14 15:59

@母鸡轰: 另外可以检查一下反向代理,并且要注意大小写。

支持(0) 反对(0) 河图书卦 | 园豆:206 (菜鸟二级) | 2018-06-14 16:06

@河图书卦: 第18行 BuildWebHost(args).Run();,代码中无中文,在window下是正常的

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 16:09

@母鸡轰: 你@dudu大佬看看错误日志,他接触.net core比我们时间长的多,经验丰富。我刚毕业,对很多东西都了解不够,尴尬的是现在在回家的车上,没法用电脑部署测试,到家也明天了,不好意思哈。回家一定按你的配置验证一下。

支持(0) 反对(0) 河图书卦 | 园豆:206 (菜鸟二级) | 2018-06-14 16:23

@河图书卦: 好,谢谢了,这个是我代码的源码https://files.cnblogs.com/files/jhy55/Lottery.Img.zip

支持(0) 反对(0) 母鸡轰 | 园豆:74 (初学一级) | 2018-06-14 16:28
0

请问怎么解决的?

Steven保持学习 | 园豆:202 (菜鸟二级) | 2021-03-04 16:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册