Score:-1

Cannot start a ngnix

kr flag

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.

Richard Smith avatar
jp flag
See [this Q&A](https://stackoverflow.com/questions/41766195/nginx-emerg-server-directive-is-not-allowed-here/41766811#41766811) over on SO.
kr flag
I tried [this](https://pastebin.com/raw/weqgti9B), `brew services restart nginx` followed by `brew services list` still returned error.
us flag
What is the content of nginx error log? What is the output of `nginx -T`, note the capital `T`?
kr flag
`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?
Score:1
us flag

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.

kr flag
I tried to use the [default configuration of nginx](https://pastebin.com/raw/aHwcV8qD), but `brew services restart nginx` followed by `brew services list` still showed error.
Score:0
kr flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.