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

3
我正在尝试将默认的Blazor Hello World应用程序(https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro)提供给公共互联网。我正在通过在Ubuntu 20.04 LTS安装中运行NginX来实现这一目标。
我使用命令“dotnet run”运行Hello World应用程序。这使得应用程序在localhost:5000可用。然后,我使用NginX将任何请求传递到服务器的公共IP地址到localhost 5000。但是,我只能获得原始的HTML文件,而没有任何所需的.css或.js文件。 图片:未加载CSS或JS文件的HTML页面 因此,当通过localhost:5000查看应用程序时,可以正常工作。但是,通过公共IP访问时,只显示原始的HTML文件,没有js或css文件。例如,通过localhost:5000/counter查看应用程序时,计数器页面会加载。但是,通过公共互联网使用IP/counter访问时,没有任何东西被加载。
这是我的nginx配置文件,在/etc/nginx/sites-available/default目录下。 我不确定需要更改什么或者在哪里可以找到更多关于此的信息。

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

1个回答

0

这是我在我的临时测试平台(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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接