微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nginx多个服务器块侦听同一个端口

我想在同一端口80上运行www.example.com和api.example.com.

这就是我所拥有的所有我的谷歌ping导致以下代码.但是,这不行.

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        location / {
                # First attempt to serve request as file,then
                # as directory,then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/Nginx/naxsi.rules
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

server {
        listen 80
        server_name api.example.com

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

我不知道原因有什么建议吗?

谢谢!

最佳答案
在/etc/Nginx/sites-available/www.example.com和/etc/Nginx/sites-available/api.example.com中分别创建两个文件(您不必要,但会更清晰)

api.example.com文件内容

server {
        listen 80
        server_name api.example.com
        root /var/www/api.example.com/html/example/app; #also add a root dir here
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

www.example.com的内容

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        location / {
                # First attempt to serve request as file,then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/Nginx/naxsi.rules
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

并最终启用域:
sudo ln -s /etc/Nginx/sites-available/www.example.com /etc/Nginx/sites-enabled/www.example.com和sudo ln -s /etc/Nginx/sites-available/api.example. com /etc/Nginx/sites-enabled/api.example.com

原文地址:https://www.jb51.cc/nginx/434619.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐