1. 程式人生 > 其它 >Linux Nginx安裝及多域名共享80埠配置

Linux Nginx安裝及多域名共享80埠配置

文章目錄

環境

Ubuntu: 20.04 LTS
Nginx: nginx version: nginx/1.18.0 (Ubuntu)

轉載請註明, 轉自 Canney : https://blog.csdn.net/canney_chen/article/details/114299853

安裝Nginx

$ sudo apt install nginx

驗證安裝版本

$ sudo nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

啟動Nginx

$ sudo nginx

驗證NGINX Open Source是否已啟動並正在執行

$ curl -I 127 .0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 03 Mar 2021 01:40:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 03 Mar 2021 01:31:55 GMT
Connection: keep-alive
ETag: "603ee70b-264"
Accept-Ranges: bytes

多域名共用80埠配置

配置檔案為nginx.conf, 一般安裝完,根據系統不同,可能存放位置(/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.) [5]

#應用一
server {
   listen 80;
   server_name app1.domain.com;
   location / {
      proxy_pass        http://app1/;
      proxy_redirect    off;
      proxy_set_header  Host  $host;
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}
#應用二
server {
   listen 80;
   server_name app2.domain.com;
   location / {
      proxy_pass        http://app2/;
      proxy_redirect    off;
      proxy_set_header  Host  $host;
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

#負載均衡節點配置
upstream app1 {
     server 192.168.1.1;
     server 192.168.1.2;
     ip_hash;
 }
upstream app2 {
     server 192.168.1.3;
     server 192.168.1.4;
     ip_hash;
}

Nginx操作幫助

$ sudo nginx -h
nginx version: nginx/1.18.0 (Ubuntu)
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

伴手禮

Linux檢視IP方法 [6]

$ ip addr show

ip addr show
inet開頭的為IP。

引用

[1] Nginx安裝參考: https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/ Installing Prebuilt Ubuntu Packages
[2] Nginx多域名共用80埠: http://jimolonely.github.io/2019/10/12/server/012-nginx-multi-domain-server/
https://blog.csdn.net/u010028869/article/details/86426635
[3] Nginx Server配置官方說明: https://docs.nginx.com/nginx/admin-guide/web-server/web-server/ Configuring NGINX and NGINX Plus as a Web Server
[4] Nginx 代理配置: https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
[5] Nginx 配置檔案位置: https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/ Creating NGINX Plus and NGINX Configuration Files
[6] Linux檢視IP方法