Nginx多个服务器块监听相同的端口 [英] Nginx multiple server blocks listening to same port

查看:1655
本文介绍了Nginx多个服务器块监听相同的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to run www.example.com and api.example.com on same port 80.

这就是我所拥有的.我所有的Google Ping都导致以下代码.但是,这不起作用.

This is what I have. All my googles ping lead to the below code. But, this is not working.

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;
        }
}

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

I do not know the reason. Any suggestions on this?

谢谢!

推荐答案

分别在/etc/nginx/sites-available/www.example.com/etc/nginx/sites-available/api.example.com

api.example.com文件的内容

The api.example.com file's conten

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的内容:

The www.example.com's content:

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;
        }
}

最后启用域: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.comsudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

这篇关于Nginx多个服务器块监听相同的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆