从本地网络访问 nginx

从本地网络访问 nginx

我在 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>

答案1

您可以在 LAN 中想要访问您网站的计算机上通过以下行添加10.120.11.110 example.com/etc/hosts(或者C:\Windows\System32\drivers\etc\hosts如果在 Windows 上)

答案2

如果您有多个主机(例如,教室环境或您没有足够的权限),使用 Hosts 文件会很繁重。

实现此目的的一种更简单、更快捷的方法是使用https://nip.io/,你不需要在 hosts 文件中做任何事情只需将您的 nginx 配置从 example.com 更改为 10-120-11-110.nip.io。

然后,您可以直接在浏览器中使用此 URL(10-120-11-110.nip.io)进行访问,无需主机文件条目。如果您感兴趣的话,nip.io 网站上还有一些其他示例。

这使得虚拟主机的测试变得轻松。

相关内容