SoFunction
Updated on 2025-03-01

Mac uses Nginx to set up proxy and disable problem records with own Apache

Mac comes with Apache service and occupies 80 ports. If you need to use Nginx, you need to disable Apache and install Nginx yourself.

1. Disable the built-in Apache

1. Close Apache

sudo apachectl -k stop

If the following error occurs:
httpd: Could not reliably determine the server’s fully qualified domain name, using . Set the ‘ServerName’ directive globally to suppress this message

(1) Open the apache configuration directory:

vim /etc/apache2/

(2) Search: #ServerName:80
Add one line ServerName localhost:80
(3) Restart Apache

sudo apachectl restart

(4) Close Apache

sudo apachectl -k stop

2. Prohibit Apache self-start

sudo launchctl unload -w /System/Library/LaunchDaemons/

2. Use Homebrew to install Nginx

When installing Nginx using Homebrew on macOS, the default installation directory for Nginx is usually/usr/local/Cellar/nginx/. The specific installation path can be viewed through the following command:

brew --prefix nginx

1. Nginx configuration files and directories

After installation, the main configuration files and directories of Nginx are usually located at the following locations:

  • Main configuration file/usr/local/etc/nginx/
  • Site configuration file/usr/local/etc/nginx/servers/
  • Log files/usr/local/var/log/nginx/
  • HTML files/usr/local/var/www/

2. Install Nginx

Install Nginx using Homebrew:

brew install nginx

3. Start and manage Nginx

After the installation is complete, you can start, stop and restart Nginx using the following commands:

# Start Nginxbrew services start nginx
# Stop Nginxbrew services stop nginx
# Restart Nginxbrew services restart nginx

4. Configure Nginx

You can edit Nginx's main configuration file/usr/local/etc/nginx/, or in/usr/local/etc/nginx/servers/Add a new site configuration file to the directory.

For example, create a new site configuration file/usr/local/etc/nginx/servers/

server {
    listen 80;
    server_name ;
    # Redirect all HTTP requests to HTTPS, if necessary, force https to enable this item    # return 301 https://$host$request_uri;
    location / {
		proxy_pass http://localhost:9000;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
	}
}
server {
    listen 443 ssl;
    server_name ;
    ssl_certificate /usr/local/etc/nginx/certs/;
    ssl_certificate_key /usr/local/etc/nginx/certs/;
    location / {
        proxy_pass http://localhost:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

5. Verify the configuration and restart Nginx

(1) Verify whether the Nginx configuration is correct (if you are not sure whether the configuration is correct, this command is very effective):

nginx -t

(2) If the configuration is correct, restart Nginx to apply the changes:

brew services restart nginx

3. Generate ssl/self-signed certificate

Open the command line tool in the folder you specified

# x509 Generate a self-signed root certificate based on existing certificate requests# -days Set the valid number of days for the certificate# rsa:2048 Modern SSL/TLS configurations usually require a key of at least 2048 bitsopenssl req -newkey rsa:2048 -nodes -keyout  -x509 -days 365 -out 

Country Name (2 letter code) [Country]:CN

State or Province Name (full name) [Province]:Beijing

Locality Name (eg, city) [City]:Beijing

Organization Name (eg, company) [Organization/Company]:test

Organizational Unit Name (eg, section) [Department/Unit]:test

Common Name (eg, fully qualified host name) [Domain name]:

Email Address [Email]:test@

This is the article about setting up a proxy with Nginx by Mac and disabling Apache with its own. For more related content on Mac using Nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!