Configure Nginx for backend server and understand configuration files
1. Configure Nginx for Backend server
Create a FastAPI project with Poetry:
FastAPI project with Poetry
Create Nginx Server Block Configuration
$ sudo nano /etc/nginx/sites-available/fastapi-poetry.conf
Enter the following code:
server {
listen 80;
listen [::]:80;
server_name marketing-online.api;
location / {
proxy_pass http://your_server_ip:8000;
proxy_set_header Host $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 $scheme;
}
}
Notes:
- listen 80: Lắng nghe và nhận các kết nối trên cổng 80 (HTTP Port)
- listen [::]:80: Tương tự nhưng cho địa chỉ IPv6
- server_name: Địa chỉ tên miền cho fastapi
- $host: Biến này tham chiêu đến tên host của yêu cầu gốc, "Host" header của yêu cầu từ client , hoặc tên server khớp với yêu cầu.
- X-Forwarded-Proto: cung cấp thông tin cơ bản cho các upstream server về các yêu cầu của client ( khi nó là yêu cầu http hoặc https).
- Header X-Real-IP là địa chỉ IP của client để proxy server ra quyết định xử lí hoặc ghi log thế nào với yêu cầu.
- Header X-Forwarded-For là một danh sách chứa địa chỉ IP của tất cả server mà client từng được ủy nhiệm.
- Biến $proxy_add_x_forwarded_for nhận giá trị gốc của header X-Forwarded-For được thu thập từ client và thêm địa chỉ IP của Nginx server vào cuối.
Ta có thể chuyển lệnh
proxy_set_header ra bên ngoài phạm
vi của server hay http, cho phép nó hoạt động với nhiều địa chỉ.
Enable the new server block file by creating a symbolic link from the
file to the sites-enabled directory:
$ sudo ln –s /etc/nginx/sites-available/fastapi-poetry.conf /etc/nginx/sites-enabled/fastapi-poetry.conf
Test the Nginx configuration for correct syntax:
$ sudo nginx -t
If there are no errors, the output will look like this:
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx service for the changes to take effect
$ sudo systemctl restart nginx
Check
marketing-online.api in a Web Browser
Open a browser window and navigate to marketing-online.api (or the
domain name you configured in Nginx).
Create
ssl folder for the server certificate and private key
files
$ sudo mkdir /etc/nginx/ssl
Create
marketing-online.api-key.pem and
marketing-online.api-cert.pemfiles (Chúng ta sẽ sử dụng chứng
chỉ
SSL tự ký
có giá trị là 10 năm)
sudo openssl req -x509 -sha256 -newkey rsa:2048 -keyout /etc/nginx/ssl/marketing-online.api-key.pem -out /etc/nginx/ssl/marketing-online.api-cert.pem -days 3650 -nodes
Notes: Bạn sẽ được nhắc nhở chi tiết nhận dạng chứng chỉ. Nhập tên miền
thích hợp cho Common Name:
Generating a 2048 bit RSA private key
......................+++
....+++
writing new private key to '/etc/nginx/ssl/marketing-online.api-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:VN
State or Province Name (full name) [Some-State]:Binh Duong
Locality Name (eg, city) []:Thu Dau Mot City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:KenjiNguyen
Organizational Unit Name (eg, section) []:KenjiNguyen
Common Name (e.g. server FQDN or YOUR name) []:marketing-online.api
Email Address []:*********
Open
fastapi-poetry.conf and add
ssl configuration
server {
listen 80;
listen [::]:80;
server_name marketing-online.api;
ssl on;
ssl_certificate /etc/nginx/ssl/marketing-online.api-cert.pem;
ssl_certificate_key /etc/nginx/ssl/marketing-online.api-key.pem;
# HTTPS Server Optimization
keepalive_timeout 70;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# Since version 1.9.1, NGINX uses these defaults for ssl_protocols and ssl_ciphers:
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /var/www/test_domain.com/html;
index index.html;
access_log /var/www/test_domain.com/access.log;
error_log /var/www/test_domain.com/error.log;
location / {
proxy_pass http://your_server_ip:8000;
proxy_set_header Host $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 $scheme;
}
}
Notes:
-
Các chỉ thị
ssl_protocolsvàssl_cipherscó thể được sử dụng để yêu cầu máy khách (clients) chỉ sử dụng các phiên bản mạnh và mật mã của SSL / TLS khi thiết lập kết nối. - $scheme: HTTP Scheme của truy vấn. Ví dụ: “http” hoặc “https“
Test the Nginx configuration for correct syntax:
$ sudo nginx -t
If there are no errors, the output will look like this:
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx service for the changes to take effect
$ sudo systemctl restart nginx
Check
marketing-online.api in a Web Browser
Open a browser window and navigate to
https://marketing-online.api (or the domain name you configured in
Nginx).
Notes: Chúng ta đã sử dụng chứng chỉ tự kí, do đó trình duyệt có
thể cảnh báo rằng kết nối có thể không đáng tin cậy .Bạn có thể tiến hành
an toàn bằng cách tin tưởng trang web.
Reference:
Tags:
nginx
