c# - 如何在 Ubuntu 上使用 NginX 提供 Blazor 应用程序

标签 c# .net ubuntu nginx blazor

我正在尝试提供默认的 Blazor hello world 应用程序 (https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro)
到公共(public)互联网。
我试图通过在 Ubuntu 20.04 LTS 安装上运行 NginX 来实现这一点。
我使用命令“dotnet run”运行 hello world 应用程序。这使得应用程序在 localhost:5000 可用。
然后,我使用 NginX 将任何请求传递给服务器公共(public) ip 到 localhost 5000。我没有加载所有文件,而是只获取原始 html 文件,而不需要任何 .css 或 .js 文件。
Image: Html page without css or js files loaded
因此,当通过 localhost:5000 查看应用程序时,我得到了工作页面。
通过服务器公共(public) IP 查看它时,我只得到原始 html,没有 js 或 css 文件。
例如,当通过 localhost:5000/counter 查看应用程序时,会加载计数器页面。
当使用 IP/计数器通过公共(public)互联网访问它时,什么都没有加载。
这是我在 etc/nginx/sites-available/default 下的 nginx 配置。
不确定我需要更改什么或在哪里可以找到有关此的更多信息。

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;
        }

最佳答案

这就是我在我的临时测试平台(Raspbian)上使用的。它对我有用,但我不保证从安全角度来看它是正确的,请在部署之前进行自己的审查。

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;
    }
}

关于c# - 如何在 Ubuntu 上使用 NginX 提供 Blazor 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67046179/

相关文章:

c# - 在 linqQuery 中只返回部分字符串

python - 提高 headless Selenium 的性能

ruby-on-rails - rmagick 不会在服务器上找到 imagemagick

linux - 识别进程的资源使用情况 : CPU, 内存和 I/O

c# - 举办锦标赛,如何存储所有玩家分数

javascript - 将数据从 JavaScript 传递到 C#

c# - 带有 BeginCollectionItem 的 MVC 5 动态行

c# - 为什么我的 MenuStrip 在第一次点击时出现在错误的位置?

c# - 能否返回 CryptoStream 并仍然正确处理所有内容?

.net - 在 WPF 功能区应用程序菜单中使用 DataTemplate 时会出现其他嵌套级别