首页 > 解决方案 > 如何使用 nginx 将域代理到另一个域?

问题描述

我尝试使用 nginx 部署 laravel,所以我编写了 ngix.conf,如下所示,它对我来说很好用。

server {
        listen 8000 default_server;
        listen [::]:8000 default_server;

        root /home/back-stage/public;

        index index.php index.html index.htm;

        server_name _;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass 127.0.0.1:9999;
                fastcgi_index index.php;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fixes timeouts
                fastcgi_read_timeout 600;
                include fastcgi_params;
         }
}

但是如果我想代理 127.0.1:3000 到 127.0.1:8000 呢?

我只是像下面这样重写了 nginx.conf,但它失败了。如何解决?

server {
        listen 3000 default_server;
        listen [::]:3000 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                proxy_pass http://localhost:8000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

server {
        listen 8000 default_server;
        listen [::]:8000 default_server;

        root /home/back-stage/public;

        index index.php index.html index.htm;

        server_name _;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass 127.0.0.1:9999;
                fastcgi_index index.php;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fixes timeouts
                fastcgi_read_timeout 600;
                include fastcgi_params;
         }
}

标签: nginx

解决方案


推荐阅读