Nginx

從本地網路訪問 nginx

  • September 16, 2022

我在 ubuntu 和 PHP 上執行了 nginx,並且可以在 example.com 上遠端訪問它。我正在做一些開發工作,從同一網路上的另一台機器訪問它,理想情況下希望使用埠 443。我使用靜態 IP 10.120.11.110 部分地完成了這項工作,但想使用 example.com . 如何實現?

server {
   server_name example.com 10.120.11.110;
   root /var/www/facdocs/api/public;
   index index.php index.html index.htm;
   access_log /var/log/nginx/facdocs-access.log;
   error_log /var/log/nginx/facdocs-error.log;
   add_header Access-Control-Allow-Origin *;
   add_header Access-Control-Allow-Methods "GET, OPTIONS,POST, PUT,PATCH, DELETE";
   add_header Access-Control-Allow-Headers "Content-Type, Authorization";
   add_header Access-Control-Expose-Headers "Origin";
   add_header Access-Control-Max-Age "3600";
   add_header Access-Control-Allow-Credentials "true";
   add_header 'Access-Control-Expose-Headers' 'Link';
   location / {
       try_files $uri $uri/ /index.php$is_args$args;
   }

   location ~ \.php$ {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php8.1-fpm-facdocs.sock;
       #fastcgi_pass unix:/var/run/php8.0-fpm-facdocs.sock;
       fastcgi_index index.php;
       include fastcgi.conf;
   }
   listen [::]:443 ssl; # managed by Certbot
   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
   if ($host = example.com) {
       return 301 https://$host$request_uri;
   } # managed by Certbot
   if ($host = 10.120.11.110) {
       return 301 http://$host$request_uri;
   } # managed by me
   server_name example.com 10.120.11.110;
   listen 80;
   listen [::]:80;
   return 404; # managed by Certbot
}
michael@michael-HP-EliteBook-830-G5:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
   inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
   link/ether 80:e8:2c:b0:58:23 brd ff:ff:ff:ff:ff:ff
   inet 10.120.11.110/24 brd 10.120.11.255 scope global noprefixroute enp0s31f6
      valid_lft forever preferred_lft forever
   inet6 fe80::f296:5072:9bd8:39a5/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
3: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
   link/ether d0:ab:d5:46:11:c1 brd ff:ff:ff:ff:ff:ff
   inet 10.120.11.109/24 brd 10.120.11.255 scope global dynamic noprefixroute wlp1s0
      valid_lft 6168sec preferred_lft 6168sec
   inet6 fe80::9d5d:d33:f846:7e4b/64 scope link noprefixroute
      valid_lft forever preferred_lft forever
michael@michael-HP-EliteBook-830-G5:~$
C:\Users\michael>ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

Unknown adapter Local Area Connection:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 10:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::8858:36cc:61b1:8e67%8
  IPv4 Address. . . . . . . . . . . : 10.120.11.101
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 10.120.11.1

Ethernet adapter Bluetooth Network Connection:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

C:\Users\michael>

您可以從以下行添加 LAN 中要訪問您的網站的電腦:10.120.11.110 example.com到*/etc/hosts*(或C:\Windows\System32\drivers\etc\hosts,如果在 Windows 上)

如果您有多個主機(例如,課堂環境或您沒有足夠的權限這樣做),那麼使用 Hosts 文件需要做很多工作。

實現此目的的一種更簡單快捷的方法是使用https://nip.io/,您無需在 hosts 文件中執行任何操作,只需將您的 nginx 配置從 example.com 更改為 10-120-11 -110.nip.io。

然後,您可以在瀏覽器 (10-120-11-110.nip.io) 中使用此 url 直接訪問,無需主機文件條目。如果你好奇的話,nip.io 在它的網站上有一些其他的例子。

這使得虛擬主機的測試變得輕鬆。

引用自:https://serverfault.com/questions/1110818