-1

My developer has added third-party authentications like Google, Twitter to our website. I would like to take over the development now. The first thing I need to do is to be able to test these third-party authentications in localhost. He gave me the following configuration file.

upstream mywebsite {
   server 178.62.00.00:443;
}

server {
    listen              443 ssl;
    server_name localhost;
    ssl_certificate /etc/ssl/localhost/localhost.crt;
    ssl_certificate_key /etc/ssl/localhost/localhost.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_timeout 1d;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    add_header X-Frame-Options "";
    proxy_ssl_name "www.mywebsite.io";
    proxy_ssl_server_name on;

    location ~ /socialLoginSuccess {
        rewrite ^ '/#/socialLoginSuccess' redirect;
    }

    location ~ /auth/(.*) {
        proxy_pass  https://mywebsite/myapp/auth/$1?$query_string;
        proxy_set_header Host localhost;
    }

    location / {
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_set_header    Accept-Encoding     "";
        proxy_set_header    Proxy               "";
        proxy_pass          http://localhost:3000/;

        # These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove socketio error
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
    }
}

I then put it as /usr/local/etc/nginx/nginx.conf under my MacOS. However, I could not run nginx successfully:

$ nginx -t
nginx: [emerg] "upstream" directive is not allowed here in /usr/local/etc/nginx/nginx.conf:1
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

$ brew services start nginx
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)

$ brew services list
Warning: Calling bottle :unneeded is deprecated! There is no replacement.
Please report this issue to the stripe/stripe-cli tap (not Homebrew/brew or Homebrew/core):
  /usr/local/Homebrew/Library/Taps/stripe/homebrew-stripe-cli/stripe.rb:9

256
Name                  Status  User     Plist
[email protected] stopped          
nginx                 error   softtimur /usr/local/opt/nginx/homebrew.mxcl.nginx.plist

Does anyone know how to fix this?

Edit 1:

I tried to use the default configuration of nginx, but brew services restart nginx followed by brew services list still showed error.

4
  • See this Q&A over on SO. Nov 1, 2021 at 21:05
  • I tried this, brew services restart nginx followed by brew services list still returned error.
    – SoftTimur
    Nov 1, 2021 at 21:13
  • What is the content of nginx error log? What is the output of nginx -T, note the capital T? Nov 3, 2021 at 21:22
  • nginx -T returns nginx: [emerg] cannot load certificate key "/etc/ssl/localhost/localhost.key": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/etc/ssl/localhost/localhost.key','r') error:2006D002:BIO routines:BIO_new_file:system lib) nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed. sudo nginx -T returns nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful. Is there anywhere I could see more about the nginx error log?
    – SoftTimur
    Nov 3, 2021 at 22:10

2 Answers 2

2

The configuration snippet given by your colleague should reside inside http block.

However, when you put that into nginx.conf and use that as a nginx configuration, it doesn't work because the http level is missing.

nginx often ships with a nginx.conf file, which has good defaults, and then include inside the http section to include other configuration.

You need to check the nginx.conf shipped with your nginx install, and see how it includes other config files.

1
1

It seems that brew services start nginx, brew services list, brew services stop nginx are not reliable here.

By contrast, sudo nginx (to start nginx) and sudo nginx -s stop work for me.

0

Not the answer you're looking for? Browse other questions tagged .