我有多个本地站点,我想将 nginx 配置为每个网站都有不同的主机。
在 /var/www 中我有 2 个站点:site1 和 site2
然后在 /etc/nginx/sites-available/ 中,我为每一个创建了 2 个不同的配置服务器。我有文件 site1 和 site2 ,其内容如下:
server {
listen 80;
root /var/www/site1;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
}
Run Code Online (Sandbox Code Playgroud)
和
server {
listen 7777;
root /var/www/site2;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
}
Run Code Online (Sandbox Code Playgroud)
http://localhost:80我通过for site1 和for site2访问它们http://localhost:7777。效果很好。我还可以在 /etc/hosts 中添加主机名,如下所示:
127.0.0.1 localhost site1 site2
Run Code Online (Sandbox Code Playgroud)
我可以使用http://site1:80和访问它们http://site2:7777。但我必须始终访问端口号。我想通过http://site1和访问它们http://site2。
有解决办法吗?
您已经弄清楚了,但让我解释一下为什么它有效。
第一个站点site1应该可以正常工作,因为默认http端口是 80,而这就是site1正在监听的端口,所以http://site1.com应该可以正常工作。
第二个配置文件是site2监听端口7777,所以正常操作http://site2.com是行不通的,实际上它可能会选择你的默认网站并提供服务,因为 nginx 不会尝试将其server_name与配置中的匹配,因为端口不匹配。
您应该在端口 80 上创建所有网站,nginx 将自行进行匹配并知道要服务器的网站,除非它是网站,否则https您将使用端口443,这是默认的 ssl 端口
| 归档时间: |
|
| 查看次数: |
19792 次 |
| 最近记录: |