简体   繁体   English

如何在 Ubuntu 上使用 NginX 为 Blazor 应用程序提供服务

[英]How to serve a Blazor app using NginX on Ubuntu

I'm trying to serve the default Blazor hello world app (https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro ) to the public internet.我正在尝试将默认的 Blazor hello world 应用程序(https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro )提供给公共互联网。 I'm trying to make this happen by running NginX on a Ubuntu 20.04 LTS installation.我试图通过在 Ubuntu 20.04 LTS 安装上运行 NginX 来实现这一点。

I run the hello world app using the command "dotnet run".我使用命令“dotnet run”运行 hello world 应用程序。 This makes the app available at localhost:5000.这使得应用程序在 localhost:5000 可用。 I then use NginX to pass any requests to the servers public ip to localhost 5000. Instead of loading all files, I only get the raw html file, without any of the.css or.js files required. I then use NginX to pass any requests to the servers public ip to localhost 5000. Instead of loading all files, I only get the raw html file, without any of the.css or.js files required.

Image: Html page without css or js files loaded图片:Html 页面未加载 css 或 js 文件

So when viewer the app trhoug localhost:5000, I get the working page.因此,当通过 localhost:5000 查看应用程序时,我得到了工作页面。 When viewing it throug the servers public IP, I only get the raw html, without js or css files.通过服务器公共 IP 查看它时,我只得到原始 html,没有 js 或 css 文件。 When viewing the app through localhost:5000/counter for example, the counter page gets loaded.例如,当通过 localhost:5000/counter 查看应用程序时,会加载计数器页面。 When accessing it via the public internet using IP/counter, nothing gets loaded.当使用 IP/计数器通过公共互联网访问它时,什么都没有加载。

This is my nginx configuration under etc/nginx/sites-available/default.这是我在 etc/nginx/sites-available/default 下的 nginx 配置。 Unsure on what I need to change or where I can find more information on this.不确定我需要更改什么或在哪里可以找到有关此的更多信息。

 server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. proxy_pass http://localhost:5000/; try_files $uri $uri/ /css /wwwroot /Shared /Services =404; 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; }

This is what I'm using on my temporary test platform (Raspbian).这就是我在我的临时测试平台(Raspbian)上使用的。 It works for me, but I make no guarantees that it's correct from a security standpoint, please do your own review before deploying.它对我有用,但我不保证从安全角度来看它是正确的,请在部署之前进行自己的审查。

server {
    listen 80;
    server_name xxxxx.duckdns.org;
    return 301 https://$host$request_uri;
}

server
{
    listen              443 ssl;
    server_name         xxxxx.duckdns.org;
    keepalive_timeout   70;

    ssl_certificate     /path/to/certs/xxxxx.duckdns.org/cert.pem;
    ssl_certificate_key /path/to/certs/xxxxx.duckdns.org/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_dhparam         /etc/nginx/ssl/dhparam.pem;
    add_header          Strict-Transport-Security "max-age=63072000; includeSubdomains";
    add_header          X-Frame-Options DENY;

    location /
    {
        proxy_pass http://localhost:5000;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM