Home  >  Q&A  >  body text

nodejs-express - Use nginx to configure port 80 of a certain domain name. If the root path cannot obtain the static file, it will be forwarded to port 9999 provided by nodejs. How to achieve this?

The code I am writing now is as follows, but it doesn’t work.
Do you need to write rewrite rules, use regular expressions, or if?

And: If you configure the static file directory in nodejs, app.use(express.static(__dirname));In this way, will the performance be lower than nginx's direct judgment and processing?

server {
        listen 80;
        server_name xxx.wechattest.com;
        root /path/to/static/root;
        index index.html;
        
        location / {

                proxy_pass http://127.0.0.1:9999;//nodejs service is running on port 9999

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                client_max_body_size 100M;
                client_body_buffer_size 1m;
                proxy_intercept_errors on;
                proxy_buffering off;
                proxy_buffer_size 128k;
                proxy_buffers 256 16k;
                proxy_busy_buffers_size 256k;
                proxy_temp_file_write_size 256k;
                proxy_max_temp_file_size 0;
                proxy_read_timeout 300;
        }
}
我想大声告诉你我想大声告诉你2713 days ago720

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-16 17:22:54

    location / {
        try_files $uri @nodejs;
    }
    
    location @nodejs {
    
                    proxy_pass http://127.0.0.1:9999;//nodejs service is running on port 9999
    
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
                    client_max_body_size 100M;
                    client_body_buffer_size 1m;
                    proxy_intercept_errors on;
                    proxy_buffering off;
                    proxy_buffer_size 128k;
                    proxy_buffers 256 16k;
                    proxy_busy_buffers_size 256k;
                    proxy_temp_file_write_size 256k;
                    proxy_max_temp_file_size 0;
                    proxy_read_timeout 300;
            }

    reply
    0
  • 为情所困

    为情所困2017-05-16 17:22:54

    You can set the 404 status to be forwarded. In addition, your current configuration uses a reverse proxy directly, and all traffic to port 80 is redirected to the proxy server on port 9999.

    reply
    0
  • 为情所困

    为情所困2017-05-16 17:22:54

    You can use try_files or error_page commands to achieve the effect you want.

    At the same time, proxy_store can also be used to achieve completely static pages.

    reply
    0
  • Cancelreply