Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using Nginx in Mac to realize Port 80 forwarding Port 8080

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Due to the reasons of the project itself, the development must be implemented on port 80, but non-Root users in the Unix kernel cannot directly use ports below 1024. At first, the author found the way of pfctl to forward port 80 to port 8080 to achieve access. After personal testing, the most serious problem occurred. After being configured according to the method at that time, it was found that the native Mac could be accessed through localhost/ and 127.0.0.1 /. It was very excited at this time. However, the most fundamental problem is that it can not be accessed with other machines in the LAN by accessing the IP address / (for example: IP address is 192.168.0.106, then other machines visit 192.168.0.106 /). Originally, it is intended to achieve parallel development and testing of the same network segment, but the result that other machines cannot access means that this time the bricks are carried out by bamboo baskets.

Later, the author made use of Nginx to forward port 80 to port 8080 of Tomcat to achieve access. The author tested it personally and said no more nonsense. Here are the detailed configuration steps:

Environment

Mac OS version: 10.12.6

Nginx version: 1.12.1

Installation

The author installs the library using Homebrew

Brew search nginxbrew install nginx

After this installation, you will find that the listening port of nginx is 8080, not 80, so you need to change the listening port of Nginx, so this step is very critical. You cannot change it in the traditional way. Please take a closer look at the section.

Because Mac OS comes with Apache service, it occupies port 80, first of all, you need to change the listening port of Apache to something else or uninstall it directly. The author is to change its listening port to 8011.

Command Lin

Sudo vim / etc/apache2/httpd.conf

The red box in the picture opens the original port 80, but the author has changed it to 8011.

Apache_conf.png

Restart sudo / usr/sbin/apachectl restart after the change

At this point, you have released port 80.

The second thing you need to know is the plist file. Plist means property list format, the format that Apple uses to save application data, which is actually a xml. You can find the corresponding plist file for nginx under / usr/local/opt/nginx, such as homebrew.mxcl.nginx.plist on the author's computer.

Its contents are as follows:

Labelhomebrew.mxcl.nginxRunAtLoadKeepAliveProgramArguments/usr/local/opt/nginx/bin/nginx-gdaemon off;WorkingDirectory/usr/local

You need to copy this file to / Library/LaunchDaemons, or ~ / Library/LaunchAgents, but there is a difference between the two.

The former starts when the system starts, and the latter starts when the user logs in. Then execute launchctl load-w, as follows:

Sudo cp / usr/local/opt/nginx/*.plist / Library/LaunchDaemons

Sudo launchctl load-w / Library/LaunchDaemons/homebrew.mxcl.nginx.plist

Finally, restart your machine, you will find that nginx is started on port 80, try to access directly through http://localhost, if the following picture appears, it means that your Nginx port 80 has been configured, which means that you have completed 60% of the work.

If your Tomcat port is 8080, you need to start it at this time. The reason why you start Tomcat at this time is to let Tomcat occupy port 8080 first, because when configuring Nginx forwarding later, you need to configure port 8080 forwarding, in order not to be preempted by Nginx at that time to occupy port 8080 (you can also not start Tomcat here, as long as you do not start the nginx service immediately after configuring nginx forwarding configuration. Because starting it directly takes up port 8080 for forwarding, but you haven't used port 8080 yet) it leads to the failure of Tomcat startup, so you can let Tomcat occupy the port first and then map and forward Nginx to port 8080.

At this point, your Tomcat has been started, and we begin to configure the forwarding configuration of Nginx:

Command Lin

Sudo vim / usr/local/etc/nginx/nginx.conf

You only need to care about the configuration in the server node.

Nginx_conf.png

Nodes under server:

Listen: listening on port 80

Server_name: which address to forward to?

Proxy_pass: which address is the agent to?

After several of these items have been configured, we will restart Nginx (restart needs to be changed to the Nginx directory) sudo / usr/local/Cellar/nginx/1.12.1/bin/nginx-s reload

All the operations are done here, and now you and your other partners on the same network segment can access your Tomcat project by directly accessing the IP address.

Related commands operate Apache commands

Stop service: sudo / usr/sbin/apachectl stop

Enable the service: sudo / usr/sbin/apachectl start

Restart the service: sudo / usr/sbin/apachectl restart

Nginx command (need to be executed under the directory of Nginx, directory: / usr/local/Cellar/nginx/1.12.1/bin/)

Enable: sudo nginx

Restart: sudo nginx-s reload

Close

First, query the nginx main process number (the process with master identity): ps-ef | grep nginx

Stop the sudo kill-QUIT main process number normally

Quick stop sudo kill-TERM main process number

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report