Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

django - 如何为Angular和Django设置Nginx服务器(How to setup Nginx server for angular and django)

I'm trying to deploy a web application to my server.

(我正在尝试将Web应用程序部署到服务器上。)

I have put the html files in one folder and I have a django server running on the same server.

(我将html文件放在一个文件夹中,并且我在同一台服务器上运行了django服务器。)

I am using nginx to set up reverse proxy for the backend but for some reason I'm not able to route to backend urls.

(我正在使用nginx为后端设置反向代理,但是由于某些原因,我无法路由到后端url。)

Here is my nginx configuration:

(这是我的nginx配置:)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://example.com$request_uri;
}
server {
        listen [::]:443 ssl ipv6only=on;
        listen 443 ssl;
        server_name example.com example.com;

    root /var/www/html/;
    index index.html;

        # Let's Encrypt parameters
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        try_files $uri $uri/ = index.html;
    }
        location /api {
          proxy_pass         http://unix:/run/gunicorn.sock;
          proxy_redirect     off;

          proxy_set_header   Host              $http_host;
          proxy_set_header   X-Real-IP         $remote_addr;
          proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Proto https;
        }
}

In the first block..I'm setting fallbacks to index.html because it is an angular app.

(在第一个程序段中。我将备用设置为index.html,因为它是一个有角度的应用程序。)

The angular app runs fine.

(角度应用程序运行正常。)

But I'm not able to access the routes of the reverse proxy server, whenever I hit a route with /api/something it takes me back to the angular app ie index.html

(但是,每当我使用/ api / something命中一条路由时,我就无法访问反向代理服务器的路由,这会使我回到有角度的应用程序,即index.html)

  ask by Goutam B Seervi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...