【问题标题】:asp.net core on linux with nginx routing doesn't work带有 nginx 路由的 Linux 上的 asp.net 核心不起作用
【发布时间】:2018-07-31 04:24:12
【问题描述】:

我创建了一个ASP.NET Core MVC 应用程序并将其部署到Linux 服务器中。当我访问 sitename.com 时,浏览器会毫无问题地显示主页/索引页面。

但是当我尝试使用sitename.com/Home/Index 或其他控制器如sitename.com/Admin/Login 时,nginx 会抛出404 Not Found 错误。应该是什么问题?

这是我的Startup.cs/Configure 方法。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

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

        app.UseStaticFiles();

        app.UseSession();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

这是来自sites-available 文件夹的我的网站配置

server {
   listen 80 default_server;
   listen [::]:80 default_server ipv6only=on;

   root /var/www/sitename.com;
   index index.html index.htm;

   server_name sitename.com www.sitename.com;

   location / {
      try_files $uri $uri/ =404;
      proxy_pass http://127.0.0.1:5000;
   }

nginx.conf

    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;

    events {
        worker_connections 768;
    }

    http {

        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;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }

    mail {

    }

【问题讨论】:

  • 如果您将这些请求直接发送到 Kestrel,它们是否有效?
  • 当我在 bash 上运行这些行时,我得到了正确的 html 响应。 curl http://localhost:5000/Home/Indexcurl http://localhost:5000/Admin/Login @AlexeyAndrushkevich
  • 那么值得分享您的nginx.conf 文件,看看它是否配置正确。
  • 我已将配置文件添加到我的问题@AlexeyAndrushkevich
  • 您是否尝试删除try_files $uri $uri/ =404;?对我来说,这看起来像是在测试某个 url 是否存在,如果不存在则返回 404。但是 /Home/Index 是一个路由,它不会映射到现有文件而是映射到控制器操作

标签: linux nginx asp.net-core asp.net-core-mvc asp.net-core-1.1


【解决方案1】:

删除try_files $uri $uri/ =404;,因为它正在测试文件系统上是否存在某个url,如果不存在则返回404。

/Home/Index 是一个路由,它不会映射到现有文件而是映射到控制器操作,因此会出现 404 错误。

【讨论】:

    【解决方案2】:

    帮助用户在 Google 上进行搜索

    我得到了 404,但我意识到 ASP Net 只接受 1 个名称的服务器

    示例不可能

    server{
        listen 80;
        listen [::]:80;
    
        server_name example.com;
    
        location /asp_app_ONE {
            proxy_pass     http://0.0.0.0:3001;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
        location /asp_app_TWO{
            proxy_pass         http://0.0.0.0:3002;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }
    

    示例确定:

    server{
        listen 80;
        listen [::]:80;
    
        server_name appONE.example.com;
    
        location / {
            proxy_pass     http://0.0.0.0:3001;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }
    server{
        listen 80;
        listen [::]:80;
    
        server_name appTWO.example.com;
    
        location / {
            proxy_pass     http://0.0.0.0:3002;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-06
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      相关资源
      最近更新 更多