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

How to install Nginx as a reverse proxy for Apache on FreeBSD 10.2

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how to install Nginx on FreeBSD 10.2 as a reverse proxy for Apache. I believe most people don't know much about it, so share this article for your reference. I hope you will get a lot after reading this article. Let's go and learn about it.

Nginx is a free and open source HTTP and reverse proxy server, which can also be used as a mail proxy server for POP3/IMAP. Nginx is a high-performance Web server, which is characterized by rich functions, simple structure and low memory consumption. The * versions were released by Igor Sysoev in 2002 and are now in use by many large technology companies, including Netflix, Github, Cloudflare, WordPress.com and so on.

Here we will "install and configure the Nginx network server as the reverse proxy for Apache on the freebsd 10.2 system." Apache will run PHP on port 8080, while we will configure Nginx to run on port 80 to receive requests from users / visitors. If port 80 receives a web page request from the user's browser, Nginx passes the request to the Apache web server and PHP running on port 8080.

prerequisite

◆ FreeBSD 10.2

◆ Root permissions

Step 1: update the system

Log in to your FreeBSD server using SSH authentication and update your system with the following command:

Freebsd-update fetch freebsd-update install

Step 2: install Apache

Apache is the open source and most widely used web server. Apache is not installed by default in FreeBSD, but we can install it directly through ports or software packages under / usr/ports/www/apache24, or we can install it directly from the FreeBSD library using the pkg command. In this tutorial, we will use the pkg command to install from the FreeBSD software library:

Pkg install apache24

Step 3: install PHP

Once Apache is successfully installed, PHP is then installed, which is responsible for processing user requests for PHP files. We will use the following pkg command to install PHP:

Pkg install php56 mod_php56 php56-mysql php56-mysqli

Step 4: configure Apache and PHP

Once everything is installed, we will configure Apache to run on port 8080 and let PHP work with Apache. To configure Apache, we can edit the configuration file "httpd.conf". For PHP, we only need to copy the PHP configuration file php.ini in the "/ usr/local/etc/" directory.

Go to the "/ usr/local/etc/" directory and copy the php.ini-production file to php.ini:

Cd / usr/local/etc/ cp php.ini-production php.ini

Next, configure Apache by editing the "httpd.conf" file in the Apache directory:

Cd / usr/local/etc/apache24 nano-c httpd.conf

The port is configured on line 52:

Listen 8080

The server name is configured on line 219:

ServerName 127.0.0.1:8080

On line 277, add the DirectoryIndex file, which Apache will use to serve requests for directories:

DirectoryIndex index.php index.html

Under line 287, configure Apache and add script support:

SetHandler application/x-httpd-php SetHandler application/x-httpd-php-source

Save and exit.

Now use the sysrc command to add the Apache startup project for boot:

Sysrc apache24_enable=yes

Then test the configuration of Apache with the following command:

Apachectl configtest

If there is no problem to get here, then start Apache!

Service apache24 start

If all is done, create a phpinfo file under the "/ usr/local/www/apache24/data" directory to verify that PHP is running smoothly under Apache:

Cd / usr/local/www/apache24/data echo "" > info.php

You can now access freebsd's server IP: 192.168.1.123:8080/info.php.

Apache and PHP on Port 8080

Apache and PHP run on port 8080.

Step 5: install Nginx

Nginx can provide high-performance Web servers and reverse proxy servers with low memory footprint. In this step, we will use Nginx as the reverse proxy for Apache, so let's install it with the pkg command.

Pkg install nginx

Step 6: configure Nginx

Once Nginx is installed, in the "nginx.conf" file, we need to make a new configuration file to replace the original nginx configuration file. Change to the "/ usr/local/etc/nginx/" directory and back up the default nginx.conf file:

Cd / usr/local/etc/nginx/ mv nginx.conf nginx.conf.oroginal

You are now ready to create a new nginx profile:

Nano-c nginx.conf

Then paste the following configuration:

User www; worker_processes 1; error_log / var/log/nginx/error.log; events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log / var/log/nginx/access.log Sendfile on; keepalive_timeout 65; # Nginx cache configuration proxy_cache_path / var/nginx/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m; proxy_temp_path / var/nginx/cache/tmp; proxy_cache_key "$scheme$host$request_uri"; gzip on; server {# listen 80; server_name _; location / nginx_status {stub_status on; access_log off } # redirect server error pages to the static page / 50x.html # error_page 500 502 503 504 / 50x.html; location = / 50x.html {root / usr/local/www/nginx-dist;} # proxy the PHP scripts to Apache listening on 127.0.1 location 8080 # location ~\. Php$ {proxy_pass http://127.0.0.1:8080; include / usr/local/etc/nginx/proxy.conf;}} include / usr/local/etc/nginx/vhost/*;}

Save and exit.

Next, under the nginx directory, create a proxy.conf file to act as a reverse proxy:

Cd / usr/local/etc/nginx/ nano-c proxy.conf

Paste the following configuration:

Proxy_buffering on; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90 Proxy_buffers 100 8k; add_header X-Cache $upstream_cache_status

Save and exit.

* one step: create a new directory "/ var/nginx/cache" for nginx cache:

Mkdir-p / var/nginx/cache

Step 7: configure the virtual host for Nginx

In this step, we need to create a new virtual host domain "saitama.me" with the document root "/ usr/local/www/saitama.me" and the log files in the "/ var/log/nginx" directory.

The * thing we have to do is create a new directory to hold the virtual host configuration file, and the new directory we create is called "vhost". Create it:

Cd / usr/local/etc/nginx/ mkdir vhost

Create the vhost directory, and then we go to this directory and create a new virtual host file. Here I call it "saitama.conf":

Cd vhost/ nano-c saitama.conf

Paste the configuration of the virtual host as follows:

Server {# Replace with your freebsd IP listen 192.168.1.123 http://127.0.0.1:8080; 80; # Document Root root / usr/local/www/saitama.me; index index.php index.html index.htm; # Domain server_name www.saitama.me saitama.me; # Error and Access log file error_log / var/log/nginx/saitama-error.log; access_log / var/log/nginx/saitama-access.log main; # Reverse Proxy Configuration location ~\ .php$ {proxy_pass http://127.0.0.1:8080; Include / usr/local/etc/nginx/proxy.conf; # Cache configuration proxy_cache my-cache; proxy_cache_valid 10s; proxy_no_cache $cookie_PHPSESSID; proxy_cache_bypass $cookie_PHPSESSID; proxy_cache_key "$scheme$host$request_uri";} # Disable Cache for the file type html, json location ~ *. (?: manifest | appcache | html? | xml | json) ${expires-1;} # Enable Cache the file 30 days location ~ *. (jpg | png | gif | jpeg | css | mp3 | wav | swf mov | doc | pdf | xls | ppt | docx | pptx | xlsx) ${proxy_cache_valid 200 120m; expires 30d; proxy_cache my-cache; proxy_cache my-cache Access_log off;}}

Save and exit.

Next, create a new log directory "/ var/log/" for nginx and virtual hosts:

Mkdir-p / var/log/nginx/

If all goes well, create the directory saitama.me under the root of the file to use as the document root:

Cd / usr/local/www/ mkdir saitama.me

Step 8: test

In this step, we are just testing our nginx and virtual host configuration.

Test the configuration of nginx with the following command:

Nginx-t

If everything is all right, use the sysrc command to add nginx as the boot startup item, and start nginx and restart apache:

Sysrc nginx_enable=yes service nginx start service apache24 restart

When all is done, under the saitama.me directory, add a new phpinfo file to verify that php is working properly:

Cd / usr/local/www/saitama.me echo "" > info.php

Then visit the domain name: www.saitama.me/info.php.

Virtualhost Configured saitamame

Nginx runs as a reverse proxy for Apache, and PHP works as well.

This is another result:

Test the uncached .html file.

Curl-I www.saitama.me

Html with no-cache

Test a .css file that has been cached for thirty days.

Curl-I www.saitama.me/test.css

Css file 30day cache

Test the cached .php file:

Curl-I www.saitama.me/info.php

PHP file cached

It's all done.

The above is all the contents of the article "how to install Nginx as a reverse proxy for Apache on FreeBSD 10.2". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report